Class declaration

Why is it that I can declare an object of a parent class as a function or as a variable but not the same as a sub-class. The sub-class can only be declared as a function. I get the error message from the sub-class object:
Error 2 error LNK1120: 1 unresolved externals

Error 1 error LNK2019: unresolved external symbol "public: __thiscall SuperB::SuperB(void)" (??0SuperB@@QAE@XZ) referenced in function _main C

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
26
27
28
29
30
31
32
33

class SuperA
{
public:SuperA();
	   void test();

};



class Subclass:public SuperA
{
public:
	Subclass();
	void run();

};



int main()
{
	Subclass B;// this causes error
        Subclass B();//this does not cause error
        SuperA A; // no error
        SuperA A();//no error


	system("pause");
	

	return 0;
}


Both has constructors so I'm not sure whats happening
Lines 24 and 26 are not declaring objects. They're declaring functions called A and B respectively. You can't declare a default-constructed object like that.

For line 23, the linker is telling you that it can't find a default constructor for Subclass. I can see that you've declared one, but I don't see a definition anywhere in your code.

EDIT: Also, the parentheses at the end of line 4 shouldn't be there.
Last edited on
you can declare objects with () though it's not recommended for (a) confusion with functions and (b) prefer braced/uniform initialization
the problem you have is there are 2 objects of type Subclass called B, you also have 2 objects of type SuperA called A though one of them was always commented out and so you didn't face the same problem with SuperA.

http://coliru.stacked-crooked.com/a/b792cfd7f601179c
OK , thanks
I found where the issue is.
I thought that the declaration would be enough and not the definition
I did not have the function definition and now that I have read the error message again, it makes sense.
Last edited on
gunnerfunner wrote:
you can declare objects with () though it's not recommended for (a) confusion with functions and (b) prefer braced/uniform initialization

Just to be clear, if you're declaring an object, using a constructor that takes arguments, then, yes, you can use parentheses. However, if you're declaring an object using the default constructor, then you cannot use parentheses, because this will be interpreted as a function declaration.

Hence, lines 24 and 26 in the OP's code are not declaring objects. They're declaring functions.
Topic archived. No new replies allowed.