class just not working at all

i have no idea why this isn't working

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

#include <Windows.h>
#include <iostream>

class Tester
{
public:
	Tester()
	{
		std::cout << "test" << std::endl;
	}
};


int main(int argv, char** argc)
{
	
	Tester test();


	system("pause");

	return 0;
}


when i run it, it doesn't print anything to the console ??
its like the constructor isn't even being called

and this happens for every function of the class not just the constructor.
Constructor called when we create class object.Object are base of c++.
your 1 include is having problem,remove #include <Windows.h>.

Tester test();
should be replace with Tester test;//here we are creating object.
This line (18):
 
    Tester test();
is not what you need.

The compiler will accept it, because it is valid code for declaring a function named test() which will return a value of type Tester.

However, what you should have is this:
 
    Tester test;  // define an object named test 

i see, thanks.
kinda wired the compiler doesn't just assume that im making a object with 0 arguments but whatever lol
Last edited on
But how then could the compiler know if you were declaring a function test() that returns a Tester object?

There's already an unambiguous way to define an object using the default constructor:

Tester test;

It's only sensible that the unambiguous way to declare a function that takes no arguments and returns a Tester object should be:

Tester test();

"Whatever" is rarely a helpful response to a problem you don't understand.
Last edited on
kinda wired the compiler doesn't just assume that im making a object with 0 arguments but whatever lol

Well, the code was valid, it just wasn't doing the task you wanted.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

class Tester
{
public:
    Tester()
    {
        std::cout << "constructor" << std::endl;
    }
};

int main()
{
    Tester test();  // declare a function
    Tester toast;   // define an object
    test();         // call the function
}

Tester test()       // define the function
{
    std::cout << "Function test() " << std::endl;
    Tester A;
    return A;       // the function returns an object 
}

Output:
constructor
Function test()
constructor

oh yeah i didn't think of that, thanks
Topic archived. No new replies allowed.