Raised This Month: $51 Target: $400
 12% 

[C++] Some weird errors I cannot understand.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Zenith77
Veteran Member
Join Date: Aug 2005
Old 04-04-2007 , 10:50   [C++] Some weird errors I cannot understand.
Reply With Quote #1

The below project was just to help me get some practice in with C++ classes and their properties.

I get the following errors when trying to build the solution:
Quote:
------ Build started: Project: Shapes (Class practice), Configuration: Debug Win32 ------
Linking...
Shapes (Class practice).obj : error LNK2019: unresolved external symbol "public: __thiscall CSquare::CSquare(int,int)" (??0CSquare@@QAE@HH@Z) referenced in function _main
Shapes (Class practice).obj : error LNK2019: unresolved external symbol "public: __thiscall CRectangle::CRectangle(int,int)" (??0CRectangle@@QAE@HH@Z) referenced in function _main
C:\Documents and Settings\Justin\My Documents\Visual Studio 2005\Projects\Shapes (Class practice)\Debug\Shapes (Class practice).exe : fatal error LNK1120: 2 unresolved externals
Build log was saved at "file://c:\Documents and Settings\Justin\My Documents\Visual Studio 2005\Projects\Shapes (Class practice)\Shapes (Class practice)\Debug\BuildLog.htm"
Shapes (Class practice) - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Not only do I not know what this error means, I can't fix it. All help appreciated :-). As a side note, I am using MSVC++ 2005 Express edition.
__________________
Quote:
Originally Posted by phorelyph View Post
your retatred

Last edited by Zenith77; 01-15-2017 at 19:34.
Zenith77 is offline
sawce
The null pointer exception error and virtual machine bug
Join Date: Oct 2004
Old 04-04-2007 , 11:58   Re: [C++] Some weird errors I cannot understand.
Reply With Quote #2

I haven't looked at the source and what not, but it's a linking error. You've got a function declared in a header somewhere (specifically the CSquare and CRectangle (int,int) constructors) but you do not have them compiled into source anywhere.

Either you forgot to create the constructor source itself, or you forgot to add a file to the project.


V--- Hello, PM

Last edited by sawce the snail; 04-04-2007 at 12:07.
sawce is offline
PM
hello, i am pm
Join Date: Jan 2004
Location: Canalization
Old 04-04-2007 , 12:04   Re: [C++] Some weird errors I cannot understand.
Reply With Quote #3

It means that you have to write a constructor for your class.

Systematically, it means that:
1) Something called the function CSquare from class CSquare
2) No one ever defined that function (even though it was possibly declared)

When this happens, the linker complains (the linker is the program which takes the final look at your object files (which the compiler created, one object file per compilation unit, ie. per .c / .cpp file) and tries to find the functions which you call in one compilation unit and define in another, ie. link them), because it can't find the code it wants. "Referenced in function _main" means that this is where the function was orginally called.

Now, chances are that you never called the CSquare function. But when the function has the same name as the class, it is the constructor, and is implicitly always called when you create an instance of this class, either by calling operator new or by declaring it as a local variable on the stack.

Sincerely,
PM

edit: hello sawce you are too fast
__________________
hello, i am pm
PM is offline
Zenith77
Veteran Member
Join Date: Aug 2005
Old 04-04-2007 , 12:20   Re: [C++] Some weird errors I cannot understand.
Reply With Quote #4

Did anyone look at the source code :'(:

CPolygon.h:
Code:
#ifndef _CPOLYGON_H_
#define _CPOLYGON_H_

// Base class

class CPolygon
{
public:
	CPolygon();
	~CPolygon();

	virtual int Perimeter() = 0;
	virtual int Area() = 0;
	virtual void DrawToScreen() = 0;
	virtual char* Name() = 0;
};

#endif // _CPOLYGON_H_
GeneralShapes.h:
Code:
#include "CPolygon.h"

class CRectangle : public CPolygon
{
public:
	CRectangle(int length, int width);
	~CRectangle();

	virtual int Perimeter();
	virtual int Area();
	virtual void DrawToScreen();
	virtual char* Name() { return "Rectangle"; }

protected:
	int itsLength;
	int itsWidth;
};

class CSquare : public CRectangle
{
public:
	CSquare(int length, int width);
	~CSquare();

	virtual char* Name() { return "Square"; }
};

#endif // _GENERAL_SHAPES_H_
GeneralShapes.cpp
Code:
#include "GeneralShapes.h"
#include "stdafx.h"

CRectangle::CRectangle(float length, float width):
itsLength(length),
itsWidth(width);
{ /* */ }

CRectangle::~CRectangle()
{ /* */ }

CSquare::CSquare(int lenght, int width)
{
	if (length != width)
	{
		printf("\nError: Length and width must be the same for %s\n", this->Name());
		delete this;
	}
}

virtual int CRectangle::Perimeter()
{
	return itsLength + itsWidth;
}

virtual int CRectangle::Area()
{
	return itsLength * itsWidth;
}

virtual void CRectangle::DrawToScreen()
{
	printf("\n Drawing %s:\n\n", this->Name());

	int i;
	int j;
	for (i = 0; i < itsLength; i++)
	{
		for (j = 0; j < itsWidth; i++)
			printf("-");
		
		printf("\n");
	}

	printf("\n\n");
}
I do so a potential problem in the implementation of the constructors. Did I not use the correct syntax to initialize the variables or am I not allowed to leave the function blank?

Also, I had planned that the CRectangle constructor would be called before CSquare's, thus initializing the classes variables. Is this the correct way to do this?
__________________
Quote:
Originally Posted by phorelyph View Post
your retatred
Zenith77 is offline
PM
hello, i am pm
Join Date: Jan 2004
Location: Canalization
Old 04-04-2007 , 12:31   Re: [C++] Some weird errors I cannot understand.
Reply With Quote #5

Well, sawce was right, you need to right-click "Source Files" in your Project Explorer, choose Add Existing Item, and add the GeneralShapes.cpp file. Otherwise it isn't compiled in the project.

Seconldy, in GeneralShapes.h, you declare CRectangle::CRectangle to take two integer parameters, but in GeneralShapes.cpp, you define it as taking two single-precision floating point values. This will give you compiler errors.

Constructos may be blank.
I think that if CSquare is derived from CRectangle (which it is), CRectangle's constructor will automatically be called first. I think
__________________
hello, i am pm
PM is offline
Zenith77
Veteran Member
Join Date: Aug 2005
Old 04-04-2007 , 12:38   Re: [C++] Some weird errors I cannot understand.
Reply With Quote #6

Ah thanks :-). The float problem was done early in the project, when I had decided to use floats, but then later changed to ints.

However, I get these errors now:
Quote:
------ Build started: Project: Shapes (Class practice), Configuration: Debug Win32 ------
Compiling...
GeneralShapes.cpp
c:\documents and settings\justin\my documents\visual studio 2005\projects\shapes (class practice)\shapes (class practice)\generalshapes.cpp(4) : error C2653: 'CRectangle' : is not a class or namespace name
c:\documents and settings\justin\my documents\visual studio 2005\projects\shapes (class practice)\shapes (class practice)\generalshapes.cpp(4) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\documents and settings\justin\my documents\visual studio 2005\projects\shapes (class practice)\shapes (class practice)\generalshapes.cpp(7) : error C2059: syntax error : '{'
c:\documents and settings\justin\my documents\visual studio 2005\projects\shapes (class practice)\shapes (class practice)\generalshapes.cpp(7) : error C2630: ';' found in what should be a comma-separated list
c:\documents and settings\justin\my documents\visual studio 2005\projects\shapes (class practice)\shapes (class practice)\generalshapes.cpp(7) : error C2550: 'CRectangle' : constructor initializer lists are only allowed on constructor definitions
c:\documents and settings\justin\my documents\visual studio 2005\projects\shapes (class practice)\shapes (class practice)\generalshapes.cpp(7) : warning C4508: 'CRectangle' : function should return a value; 'void' return type assumed
c:\documents and settings\justin\my documents\visual studio 2005\projects\shapes (class practice)\shapes (class practice)\generalshapes.cpp(9) : error C2653: 'CRectangle' : is not a class or namespace name
c:\documents and settings\justin\my documents\visual studio 2005\projects\shapes (class practice)\shapes (class practice)\generalshapes.cpp(9) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Build log was saved at "file://c:\Documents and Settings\Justin\My Documents\Visual Studio 2005\Projects\Shapes (Class practice)\Shapes (Class practice)\Debug\BuildLog.htm"
Shapes (Class practice) - 7 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
All of this, I believe is caused by the first error. Which says CRectangle is not a class but you can clearly see:

Code:
class CRectangle : public CPolygon
__________________
Quote:
Originally Posted by phorelyph View Post
your retatred
Zenith77 is offline
commonbullet
Veteran Member
Join Date: Oct 2005
Old 04-04-2007 , 16:14   Re: [C++] Some weird errors I cannot understand.
Reply With Quote #7

This is how (I guess) I fixed it. Most changes were logged.
Attached Files
File Type: rar shapes.rar (1.9 KB, 82 views)
commonbullet is offline
Send a message via ICQ to commonbullet Send a message via MSN to commonbullet
Zenith77
Veteran Member
Join Date: Aug 2005
Old 04-04-2007 , 22:32   Re: [C++] Some weird errors I cannot understand.
Reply With Quote #8

Oh thanks, haven't tried compile yet, but what exactly are you doing here (appears about two other times)

Code:
CSquare::CSquare(int length, int width):CRectangle(length, width)
I'm referring to the ':CRectangle(lenght, width)' part. Also, would it be possible to implement the destructor before the constructor (will try this soon)?

edit
It compiles now, yay ^^. Had to fix a few other things, but still crashes when I run it. I should have it from here (hopefully), thanks for all your help everyone :-).
__________________
Quote:
Originally Posted by phorelyph View Post
your retatred

Last edited by Zenith77; 04-04-2007 at 22:54.
Zenith77 is offline
commonbullet
Veteran Member
Join Date: Oct 2005
Old 04-05-2007 , 02:33   Re: [C++] Some weird errors I cannot understand.
Reply With Quote #9

I'm not skilled in polymophism but I guess that's why you need that.

CRectangle doesn't implement a default constructor (CRectangle::CRectangle()), so you must make explicit which constructor will create an instance of that class (since CSquare is derived from CRectangle).

It also needs that to initialize itsWidth, itsLength variables because CSquare constructor won't do it.

Anyway you'd better check this info.

And ... it's working for me.

Last edited by commonbullet; 04-05-2007 at 02:38.
commonbullet is offline
Send a message via ICQ to commonbullet Send a message via MSN to commonbullet
Zenith77
Veteran Member
Join Date: Aug 2005
Old 04-05-2007 , 09:44   Re: [C++] Some weird errors I cannot understand.
Reply With Quote #10

Oh, I should have updated yesterday, was getting kind of late, it works now ^^.
I keep getting Access Violations, which was because I forgot to pass (as you noted in the log) to pass the size of the array in scanf_s() :-). The only problem now is that when attempts to draw the figure, it just goes in an infinite loop, but I'm sure I can fix this myself ;).

About the constructor, I understand why it's doing, I just wanted to know the , I guess functionality of putting the ':Myfunc()' after the other function? Like for example:

Code:
void Func():Func2()
Does it call Func2() then call Func(), or is it doing something else?

Thank you guys for all the help.
__________________
Quote:
Originally Posted by phorelyph View Post
your retatred
Zenith77 is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 01:06.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode