954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

OOp memory leak error

When running the following code i get a big compile error:
Linking...
main.obj : error LNK2001: unresolved external symbol "public: __thiscall debug::debug(int)" (??0debug@@QAE@H@Z)
Debug/main.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.


Class definiation saved as : class.h
[php]
#ifndef CLASS_H
#define CLASS_H

class debug
{
public:
debug(int a = 77);
int getbug();

private:
int x;

};

#endif
[/php]

class functions saved as: debug.cpp
[php]
#include

using namespace std;

#include "class.h"

debug::debug(a)
{
a = x;
}

int debug::getbug()
{
return x;
}
[/php]

main: saved as main.cpp

[php]
#include

using namespace std;

#include "class.h"

int main()

{

debug d1(100);

return 0;
}
[/php]

if you've noticed in main thats theres an include.h , if I change it to include the .cpp instead it works 100% but thats not considered good programming. So I'm hoping someone will let me know whats what?

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

Should it be like this?

debug::debug(int a)
{
   x = a;
}
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

I made the corectiong but no different in the error I get.

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

Is "debug.cpp" part of the project?

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

yes debug.cpp is the functions,

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 
yes debug.cpp is the functions,

I don't quite follow what you are saying. My question is whether or not the project includesboth source modules (as opposed to the mere existance of files).

[IMG]http://img97.exs.cx/img97/3200/untitled1ms.th.jpg[/IMG]

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Now I'm confused I thought the header itself will include that cpp file, so when you include the .H files its auto included?

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

A header should have declarations and allow all modules to compile. But a definition is needed to successfully link. This is where you are having problems. You tell the linker to expect a definition to be found, but then don't tell it where to find it.

I'm not sure if you looked at the picture I added to my previous reply in the edit, but your project ought to look something like that.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
main.obj : error LNK2001: unresolved external symbol "public: int __thiscall debug::getbug(void)" (?getbug@debug@@QAEHXZ) main.obj : error LNK2001: unresolved external symbol "public: __thiscall debug::debug(int)" (??0debug@@QAE@H@Z) Debug/main.exe : fatal error LNK1120: 2 unresolved externals Error executing link.exe.

I made the edits but it also failed... I'm going to have a play with working code and see if that produces something different

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

This is between you and the linker. Look at the project pane. See the two modules listed.

[edit]And stop messing with the code. The error is not there.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

So basically its my compiler? I have another code which works perfectly ... I ran that and it produced 7 liner errors, loaded up linux and built the objects and it compiles fine!! Does this mean that Microsoft VC++ is not ment for this task?

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

No.

::deep breath::

For multi-module coding, you need to have more than one module in the project. To do this you add files to the project.

Here. Go to Project->Add to Project->Files... and then add debug.cpp to your project. Then rebuild.

[edit]It ought to look like this.
[IMG]http://img30.exs.cx/img30/9715/untitled49ut.jpg[/IMG]
[/edit]

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Bang and it works :S

what compiler you using?

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

>what compiler you using?

Usually BC5.5 command line; sometimes MSVC6; sometimes Dev-C++. And ICCAVR for my current project.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You