user3668129
user3668129

Reputation: 4820

Error: prototype for ‘XXX’ does not match any in class ‘YYY’

I'm getting the following errors when adding namespace to my project:

CPoolElement.h:

#ifndef CPOOLELEMENT_H_
#define CPOOLELEMENT_H_

namespace TestName {

class CPoolElement {
public:
    CPoolElement();
    virtual ~CPoolElement();
};

}
#endif /* CPOOLELEMENT_H_ */

CPoolElement.cpp:

#include "CPoolElement.h"

namespace TestName {

CPoolElement::CPoolElement() {
    // TODO Auto-generated constructor stub

}

CPoolElement::~CPoolElement() {
    // TODO Auto-generated destructor stub
}

}

CRecordingPoolElement.cpp:

#include "CRecordingPoolElement.h"

namespace TestName {

CRecordingPoolElement::CRecordingPoolElement() {
    // TODO Auto-generated constructor stub

}

CRecordingPoolElement::~CRecordingPoolElement() {
    // TODO Auto-generated destructor stub
}

}

CRecordingPoolElement.h:

#ifndef CRECORDINGPOOLELEMENT_H_
#define CRECORDINGPOOLELEMENT_H_

#include "CPoolElement.h"

namespace TestName {

class CRecordingPoolElement : public CPoolElement{
public:
    CRecordingPoolElement();
    virtual ~CRecordingPoolElement();
};

}
#endif /* CRECORDINGPOOLELEMENT_H_ */

CTwo.h:

#ifndef CTWO_H_
#define CTWO_H_

class CPoolElement;

namespace TestName {


class CTwo {
public:
    CTwo();
    virtual ~CTwo();
    CPoolElement* GetElm();
};

}
#endif /* CTWO_H_ */

CTwo.cpp:

#include "CTwo.h"
#include "CRecordingPoolElement.h"

namespace TestName {

CTwo::CTwo() {
    // TODO Auto-generated constructor stub
}

CTwo::~CTwo() {
    // TODO Auto-generated destructor stub
}

CPoolElement* CTwo::GetElm() {
    return new CRecordingPoolElement();
}

}

The error: "error: prototype for TestName::CPoolElement* TestName::CTwo::GetElm() does not match any in class TestName::CTwo"

Upvotes: 1

Views: 968

Answers (3)

Danny_ds
Danny_ds

Reputation: 11406

class CPoolElement has to be moved inside the namespace:

CTwo.h:

namespace TestName {

class CPoolElement;

// ...
}

Upvotes: 1

MerickOWA
MerickOWA

Reputation: 7602

You forward declared "class CPoolElement" outside the "TestName" namespace, but the CPoolElement found inside your definition of CTwo::GetElm is a class which is declared in the TestName namespace.

Namespaces allow you to separate your code from other class names which might be similarly named but declared in other headers, possibly coming a library or some external dependency. These are completely different classes and could do entirely different things.

When you forward declared CPoolElement in CTwo.h, you specified that the CPoolElement class you wanted should live (be declared) in the "global" namespace. However, when the compiler encountered your declaration of GetElm, it discovered a different class, "TestName::CTwo".

Move your forward declaration into the TestName namespace and I think you'll resolve your error.

CTwo.h:

#ifndef CTWO_H_
#define CTWO_H_

//class CPoolElement; <--- NOT HERE

namespace TestName {

class CPoolElement; // <--- HERE

class CTwo {
public:
    CTwo();
    virtual ~CTwo();
    CPoolElement* GetElm();


};

}
#endif /* CTWO_H_ */

Upvotes: 4

Danke Xie
Danke Xie

Reputation: 1797

This is indeed a bit puzzling. But I think you can fix it by moving "class CPoolElement" into the namespace TestName in "CTwo.h".

This is because you defined TestName::CPoolElement in CPoolElement.h, but in CTwo.h, you are referencing ::CPoolElement. There is indeed a mismatch.

Upvotes: 1

Related Questions