Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I recently got stuck in a situation. In the first version, everything is implemented in header file and it works fine. In the second version when i tried to separate implementation from header declarations, I got many errors. In the below lines, i m going to demonstrate problem. Thanks in advance..
First Version (it works fine!)
cameravalue.h
#ifndef CAMERAVALUE_H
#define CAMERAVALUE_H
#include <string>
class CameraValue
{
private:
class CameraProperties
{
private:
CameraProperties()
: mId(-1),
mName(),
mAddress(),
mExposure(),
mFocus()
{}
int mId;
std::string mName;
std::string mAddress;
std::string mExposure;
long long mFocus;
friend class CameraValue;
friend class CameraBuilder;
};
public:
class CameraBuilder
{
public:
CameraBuilder(int id)
{
mProperties.mId = id;
}
CameraBuilder& setName(std::string& name)
{
mProperties.mName = name;
return *this;
}
CameraBuilder& setAddress(std::string& adress)
{
mProperties.mAddress = adress;
return *this;
}
CameraBuilder& setExposure(std::string& exposure)
{
mProperties.mExposure = exposure;
return *this;
}
CameraBuilder& setFocus(int focus)
{
mProperties.mFocus = focus;
return *this;
}
CameraValue build()
{
return CameraValue(mProperties);
}
private:
CameraProperties mProperties;
};
private:
CameraValue(const CameraProperties& properties)
:mProperties(properties)
{}
CameraProperties mProperties;
};
#endif // CAMERAVALUE_H
main.cpp
#include "cameravalue.h"
#include <iostream>
int main(int argc, char *argv[])
{
CameraValue cm = CameraValue::CameraBuilder(1).setName(std::string("Huseyin")).build();
return 0;
}
Second Version (Don't work)
cameravalue.h
#ifndef CAMERAVALUE_H
#define CAMERAVALUE_H
#include <string>
class CameraValue
{
private:
class CameraProperties;
public:
class CameraBuilder;
private:
CameraValue(const CameraProperties& properties);
CameraProperties mProperties;
};
#endif // CAMERAVALUE_H
cameravalue.cpp
#include "cameravalue.h"
#include <string>
class CameraValue::CameraProperties
{
private:
CameraProperties()
: mId(-1),
mName(),
mAddress(),
mExposure(),
mFocus()
{}
int mId;
std::string mName;
std::string mAddress;
std::string mExposure;
long long mFocus;
friend class CameraValue;
friend class CameraBuilder;
};
class CameraValue::CameraBuilder
{
public:
CameraBuilder(int id)
{
mProperties.mId = id;
}
CameraBuilder& setName(std::string& name)
{
mProperties.mName = name;
return *this;
}
CameraBuilder& setAddress(std::string& adress)
{
mProperties.mAddress = adress;
return *this;
}
CameraBuilder& setExposure(std::string& exposure)
{
mProperties.mExposure = exposure;
return *this;
}
CameraBuilder& setFocus(int focus)
{
mProperties.mFocus = focus;
return *this;
}
CameraValue build()
{
return CameraValue(mProperties);
}
private:
CameraProperties mProperties;
};
CameraValue::CameraValue(const CameraProperties& properties)
: mProperties(properties)
{}
main.cpp
#include "cameravalue.h"
#include <iostream>
int main(int argc, char *argv[])
{
CameraValue cm = CameraValue::CameraBuilder(1).setName(std::string("Huseyin")).build();
return 0;
}
Compile Errors
cameravalue.cpp
c:\users\huseyin\documents\builderpattern\cameravalue.h(20) : error
C2079: 'CameraValue::mProperties' uses undefined class
'CameraValue::CameraProperties' ..\BuilderPattern\cameravalue.cpp(74)
: error C2440: 'initializing' : cannot convert from 'const
CameraValue::CameraProperties' to 'int'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
..\BuilderPattern\cameravalue.cpp(74) : error C2439:
'CameraValue::mProperties' : member could not be initialized
c:\users\huseyin\documents\builderpattern\cameravalue.h(20) : see declaration of 'CameraValue::mProperties'
c:\users\huseyin\documents\builderpattern\cameravalue.h(20) : error
C2079: 'CameraValue::mProperties' uses undefined class
'CameraValue::CameraProperties' ..\BuilderPattern\main.cpp(9) : error
C2440: '<function-style-cast>' : cannot convert from 'int' to
'CameraValue::CameraBuilder'
Source or target has incomplete type ..\BuilderPattern\main.cpp(9) : error C2228: left of '.setName' must
have class/struct/union ..\BuilderPattern\main.cpp(9) : error C2228:
left of '.build' must have class/struct/union
..\BuilderPattern\main.cpp(9) : error C2512: 'CameraValue' : no
appropriate default constructor available
..\BuilderPattern\main.cpp(10) : error C2039: 'getName' : is not a
member of 'CameraValue'
c:\users\huseyin\documents\builderpattern\cameravalue.h(8) : see declaration of 'CameraValue'
Definition of class CameraBuilder should be visible in main.cpp, so you can't just forward-declare it in cameravalue.h. But you can make the definitions of its member functions out-of-line:
// cameravalue.h
class CameraValue {
class CameraBuilder {
public:
CameraBuilder(int id);
...
};
};
// cameravalue.cpp
CameraValue::CameraBuilder::CameraBuilder(int id) {
...
}
Related
I searched through some of the other pages with this same error, but my code does not have any of their issues that I can find.
I have a base class named QBase defined in quadrature.h:
#ifndef SRC_QUADRATURE_H_
#define SRC_QUADRATURE_H_
#include "enum_order.h"
#include "enum_quadrature_type.h"
#include <vector>
#include <memory>
class QBase
{
protected:
QBase (const Order _order=INVALID_ORDER);
public:
virtual ~QBase() {}
virtual QuadratureType type() const = 0;
static std::unique_ptr<QBase> build (const QuadratureType qt, const Order order=INVALID_ORDER);
const std::vector<double> & get_points() const { return _points; }
const std::vector<double> & get_weights() const { return _weights; }
std::vector<double> & get_points() { return _points; }
std::vector<double> & get_weights() { return _weights; }
protected:
const Order _order;
std::vector<double> _points;
std::vector<double> _weights;
};
#endif /* SRC_QUADRATURE_H */
I derive a class QGaussLegendre by QBase definded in gauss_legendre.h
#ifndef SRC_QUADRATURE_GAUSSLEGENDRE_H_
#define SRC_QUADRATURE_GAUSSLEGENDRE_H_
#include "quadrature.h"
class QGaussLegendre : public QBase
{
public:
QGaussLegendre (const Order _order=INVALID_ORDER) : QBase (_order){}
~QGaussLegendre (){}
virtual QuadratureType type() { return QGAUSSLEGENDRE; }
};
#endif /* SRC_QUADRATURE_GAUSSLEGENDRE_H_ */
In the main file I use the build() member function to get points and weights as follows
const Order order = ddp.order;
const QuadratureType qt = ddp.qt;
static std::unique_ptr<QBase> qr(QBase::build(qt,order));
const std::vector<double>& points = qr->get_points();
const std::vector<double>& weights = qr->get_weights();
I don't have any problem till here. Now, the points and weights are defined in the file legendre_gauss.cxx
#include "gauss_legendre.h"
QGaussLegendre::QGaussLegendre(const Order order)
{
switch(order)
{
case CONSTANT:
case FIRST:
{
_points.resize (1);
_weights.resize(1);
_points[0](0) = 0.;
_weights[0]= 2.;
}
}
}
When I compile this last file I get the error:
/home/matteo/flux/gauss_legendre.cxx:13:1:
error: redefinition of ‘QGaussLegendre::QGaussLegendre(qenum::Order)’
QGaussLegendre::QGaussLegendre(const Order order)
^~~~~~~~~~~~~~
In file included from /home/matteo/flux/gauss_legendre.cxx:8:0:
/home/matteo/flux/gauss_legendre.h:25:3:
note: ‘QGaussLegendre::QGaussLegendre(qenum::Order)’ previously
defined here
QGaussLegendre (const Order _order=INVALID_ORDER) : QBase (_order)
^~~~~~~~~~~~~~
Can I do to solve the problem? Thanks a lot.
redefinition of classes error
That's not an error about redefinition of a class. That is an error about redefinition of a function. In particular, redefinition of the function QGaussLegendre::QGaussLegendre(const Order order) which is the contsructor of class QGaussLegendre.
You've defined it first here in quadrature.h:
QGaussLegendre (const Order _order=INVALID_ORDER) : QBase (_order){}
And second time in legendre_gauss.cxx:
QGaussLegendre::QGaussLegendre(const Order order)
{
Can I do to solve the problem?
Solution is to define the function exactly once.
I have a class which implements a getter to an std::vector. Derived classes are allowed to change the content of the vector, while any other class may read it (or make a copy in my case), but not change it.
SSCCE with Visual Studio 2010 (but should compile with any other as well).
So in the base class I implemented the getter like this:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
class X
{
public:
inline std::vector<std::string> const &getChilds(void) const
{
return mChilds;
}
void mutateInternal(void)
{
mState != mState;
}
protected:
inline std::vector<std::string> &getChilds(void)
{
return mChilds;
}
private:
std::vector<std::string> mChilds;
bool mState;
};
// Now in the derived class
class Y : public X
{
public:
Y(void)
{
std::vector<std::string> &childs = getChilds();
childs.push_back("Test");
}
};
// In the non derived class:
class Z
{
public:
void myfunction(void)
{
Y y;
std::vector<std::string> s = y.getChilds();
if(s.size() == 0)
y.mutateInternal();
}
};
int main(int argc, char *argv[])
{
return 0;
}
But I get the error
1>junk.cpp(49): error C2248: "X::getChilds": cannot access private member declared in class.
1> junk.cpp(18): Siehe Deklaration von 'X::getChilds'
1> junk.cpp(10): Siehe Deklaration von 'X'
and I don't really see what is wrong with that and why the compiler doesn't take the public version which is const and instead insists on the non-const.
Even if I change the variable to const &s (which wouldn't help in this case) I still get the same error.
Update:
Edited the SSCCE for calling const and non const functions.
In this case it should be
const Y y;
in Z::my_function for call const version of function.
Live
Or just cast to const Y, like
std::vector<std::string> s = const_cast<const Y&>(y).getChilds();
Your case don't work, since access check will be applied only after overload resolution, in call y.getChilds() non-const overload will be picked, since it has the best match.
I have a class "GameOverState" which has a private member
static const std::string s_gameOverID;
In GameOverState.cpp I am initialising as :
const std::string GameOverState::s_gameOverID = "GAMEOVER";
I am getting the following errors:
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2440: 'initializing' : cannot convert from 'const char [9]' to 'int'
error C2377: 'std::string' : redefinition; typedef cannot be overloaded with any other symbol
error C2373: 's_gameOverID' : redefinition; different type modifiers
error C2143: syntax error : missing ';' before 'GameOverState::s_gameOverID'
I have a PlayState class/PauseState class which have the same implementation which are working fine. How do I fix this bug??
GameOverState.h
#pragma once
#include "GameState.h"
#include "PlayState.h"
#include "MenuState.h"
#include "PauseState.h"
#include "AnimatedGraphic.h"
#include <string>
class GameObject;
class GameOverState : public GameState
{
public:
virtual void update();
virtual void render();
virtual bool onEnter();
virtual bool onExit();
virtual std::string getStateID() const { return s_gameOverID; }
private:
static void s_gameOverToMain();
static void s_restartPlay();
static const std::string s_gameOverID;
std::vector<GameObject*> m_gameObjects;
}
GameOverState.cpp
#include "GameOverState.h"
const std::string GameOverState::s_gameOverID = "GAMEOVER";
void GameOverState::s_gameOverToMain()
{
TheGame::Instance()->getStateMachine()->changeState(new MenuState());
}
void GameOverState::s_restartPlay()
{
TheGame::Instance()->getStateMachine()->changeState(new PlayState());
}
bool GameOverState::onEnter()
{
if (!TheTextureManager::Instance()->load("assets/gameover.png", "gameovertext", TheGame::Instance()->getRenderer()))
{
return false;
}
if (!TheTextureManager::Instance()->load("assets/main.png", "mainbutton", TheGame::Instance()->getRenderer()))
{
return false;
}
if (!TheTextureManager::Instance()->load("assets/restart.png", "restartbutton", TheGame::Instance()->getRenderer()))
{
return false;
}
GameObject* gameOverText = new AnimatedGraphic(new LoaderParams(200, 100, 190, 30, "gameovertext"), 2);
GameObject* button1 = new MenuButton(new LoaderParams(200, 200, 200, 80, "mainbutton"), s_gameOverToMain);
GameObject* button2 = new MenuButton(new LoaderParams(200, 300, 200, 80, "restartbutton"), s_restartPlay);
m_gameObjects.push_back(gameOverText);
m_gameObjects.push_back(button1);
m_gameObjects.push_back(button2);
std::cout << "entering PauseState\n";
return true;
}
You're missing the semicolon after the definition of GameOverState.
The preprocessor runs before compilation and basically just copy pastes the content of the header in the source file, altough we can't see that. An error resulting from a broken header can thus be pretty misleading.
It's legal to have class definitions inside a variable definition and the position of specifiers (like static) is not limited to the beginning of a declaration, either (for example, int const static x = 0; is fine).
So, your code looks like this to the compiler:
class GameOverState {} static const std::string GameOverState::s_gameOverID = "GAMEOVER";
Hopefully the errors make more sense now.
As everyone else has said, you're probably missing the #include line in your header if your other two classes are working fine. It's presuming and expecting an int, so that seems the case.
Make sure #include<string> is in your header
I have this in furniture.h:
#include <iostream>
#include <string>
using namespace std;
class Furniture {
public:
Furniture();
virtual ~Furniture();
void setname(string name);
void setprice(double price);
int getprice();
string getname();
private:
string name;
int price;
protected:
static int NumberOfItems;
int Id;
}
and this in furniture.cpp
#include "furniture.h"
void Furniture::setname(string name) {
this->name = name;
}
string Furniture::getname()
{
return this->name;
}
void Furniture::setprice(double price) {
this->price = price;
}
int Furniture::getprice() {
return this->price;
}
int main() {
Furniture *model = new Furniture();
model->setname("FinalDestiny");
model->setprice(149.99);
cout<<"Model name: "<<model->getname()<<" - price = "<<model->getprice();
}
But I get some errors like:
Error 1 error C2628: 'Furniture' followed by 'void' is illegal (did you forget a ';'?) c:\final\facultate\poo\laborator 1\furniture.cpp 3 1 POO_lab
Error 2 error C2556: 'Furniture Furniture::setname(std::string)' : overloaded function differs only by return type from 'void Furniture::setname(std::string)' c:\final\facultate\poo\laborator 1\furniture.cpp 3 1 POO_lab
Error 3 error C2371: 'Furniture::setname' : redefinition; different basic types c:\final\facultate\poo\laborator 1\furniture.cpp 3 1 POO_lab
Error 5 error C2264: 'Furniture::setname' : error in function definition or declaration; function not called c:\final\facultate\poo\laborator 1\furniture.cpp 19 1 POO_lab
What am I doing wrong?
You are missing a ; at the end of the class definition in your header file.
// ...snipped...
protected:
static int NumberOfItems;
int Id;
}; // <-- here
You've forgotten a semicolon at the end of your class definition.
// ...
protected:
static int NumberOfItems;
int Id;
}; // <--
I hate that about C++ :)
Two things;
You're not ending your class definition with a ;, you need one at the end of furniture.h.
You've declared that there's a constructor and destructor, but neither is implemented in your .cpp file.
When I run the following code...
#ifndef KEYEDITEM_H_INCLUDED
#define KEYEDITEM_H_INCLUDED
#include <string>
typedef std::string KeyType;
class KeyedItem {
public:
KeyedItem() {}
KeyedItem(const KeyType& keyValue) : searchKey(keyValue) {}
KeyType getKey() const
{ return searchKey;
}
private:
KeyType searchKey; };
#endif // KEYEDITEM_H_INCLUDED
I get an error message "error: expected initializer before 'KeyType'"
I thought at first that this could be related to the declaring the string type so I changed it to the following to see if it would work...
#ifndef KEYEDITEM_H_INCLUDED
#define KEYEDITEM_H_INCLUDED
#include <string>
//typedef std::string KeyType;
class KeyedItem
{
public:
KeyedItem() {}
KeyedItem(const std::string& keyValue) : searchKey(keyValue) {}
std::string getKey() const
{ return searchKey;
}
private:
std::string searchKey;
};
#endif // KEYEDITEM_H_INCLUDED
but I got the error "error: multiple types in one declaration" I have looked for errors for both of these errors and have found nothing that helps. I have gone over the class to make sure I have semi-colons where needed and I seem to have all of them.
I don't have a implementation file simply because I didn't need one, but could that be the problem?
This is just a class for a binary search tree. I am working in CodeBlocks using the GNU GCC compiler.
TreeNode.h
#ifndef TREENODE_H_INCLUDED
#define TREENODE_H_INCLUDED
#include "KeyedItem.h"
typedef KeyedItem TreeItemType;
class TreeNode
{
private:
TreeNode() {}
TreeNode(const TreeItemType& nodeItem,
TreeNode *left = NULL,
TreeNode *right = NULL) : item(nodeItem), leftChildPtr(left), rightChildPtr(right) {}
TreeItemType item;
TreeNode *leftChildPtr, *rightChildPtr;
friend class BinarySearchTree;
};
#endif // TREENODE_H_INCLUDED
you need to compile with g++ not gcc
Solved... Turns out I was missing a header include in my main.