views:

277

answers:

6

Hello,

First of all, excuse me for my poor knowledge on C++. I am a real Beginner!

I am trying to compile one C++ code on MS VS2005. I am getting a linker error as follows for below piece of code:-

In one function(some class method) definition, it has code for memory allocation like:

CDecoderUnit *du = new CDecoderUnit(); //<<error is shown at this line

This CDecoderUnit is a class defined in a header file as something like

class CDecoderUnit : public IPrepareDecoderUnit
{
   CDecoderUnit();
   ~CDecoderUnit();
...
...
other class definition.

..
..
}

The actual error is:-

Error 9 error LNK2019: unresolved external symbol "public: __thiscall CDecoderUnit::CDecoderUnit(void)" (??0CDecoderUnit@@QAE@XZ) referenced in function "private: long __thiscall CLSDEnc::CreateIPrepareDecoderUnit(struct IPrepareDecoderUnit * &)const " (?CreateIPrepareDecoderUnit@CLSDEnc@@ABEJAAPAUIPrepareDecoderUnit@@@Z) lsdenc.obj

Can anyone point me to resolve this? Is any more info needed?

Thank you.

-AD.

A: 

The default accessibility in a class is private. Declare your constructor like this:

public: CDecoderUnit();
Jochen Walter
In C++, it's like public:CDecoderUnit();
Aamir
@Aamir: You are right. Thank you for pointing this out to me. (I'm using Java these days.)
Jochen Walter
But thats a compiler error, not a linker error isn't it?
Naveen
@Naween: You, too, are right. Seems like the source code given in the question is not the same as given to the compiler. Probably the problem is one in Meeh's list.
Jochen Walter
Aamir: 1.) What is the difference between what u have mentioned and what Jochen has 2.) There is no definition for the constructor anywhere i found in code. 3.) The constructor is declared in the class declaration as public: CDecoderUnit();
goldenmean
@goldenmean: 1) This is the curse of online media: I had forgotten the colon in my response and inserted it after Aamir's response. 2) The implementation file is not shown in the question, so it hard to tell whether the ctor's definition is missing.
Jochen Walter
+1  A: 

Possible problems:

  • You didn't define the constructer in the .cpp
  • You didn't include the right header in your .cpp
  • You forgot to make the constructor public (class methods are private by default)

Try to:

  • Rebuild the project
  • Check that you include the right header in the .cpp (spelling! ;) )
  • Remember that your constructor should go like this: CDecoderUnit::CDecoderUnit(){} in the .cpp
  • Add public: to the top of your class definition in the .h (Remember to do private: for your privates)
  • You forgot the ; after the class definition (Don't think this is it. The compiler ussually generates a different error for this)

To make your constructor public, declare the class like this:

class MyClass
{
public:
   //Public stuff goes here
   MyClass();
   void publicMethod();
private:
   //Private stuff goes here
};
cwap
@Meeh: 1.) There is no definition for the constructor anywhere i found in code. 2.) The constructor is declared in the class declaration aspublic: CDecoderUnit();
goldenmean
Private members are indeed not exported, but access of a member is detected at compile time, not at link time.
xtofl
+5  A: 

When you write CDecoderUnit(); you are doing 2 things:

  • Declaring a default constructor (not implementing it, just declaring it).
  • Causing the compiler not to generate a default constructor on its own.

You need to either define the constructor if it needs to initialize anything, or remove the declaration which will let the compiler generate one itself. Note that if you do implement the constructor, you'll have to make it public if you want to create objects from outside the class.

Tal Pressman
+1  A: 

Do you have the source file containing the method bodies for methods such as CDecoderUnit::CDecoderUnit() included in your project?

If so, check the properties on that source file to make sure that it is actually being used - right-click in the Solution Explorer to choose properties and check the General property Excluded from Build is set to No.

A more subtle problem can arise with line endings. The editor understands different line ending styles and will correctly show your file. The compiler only understands the Windows (CR LF) style. If the first line is a // comment then the compiler sees the entire file as being commented out. In this case, the fix is to save it as Windows style - see File - Advanced Save Options.

Andy Dent
A: 

Are these two things in two different dlls ? For example, if you have defined CDecoderUnit in one dll and creating an instance in another one. If that is the case, then you have to export the class from the first dll so as to create the object in the other one.

Naveen
A: 

Tal Pressman is right.

MadH