Passing template class instance to a constructor of another class - c++

My code:
BlockyWorld.hpp
#ifndef BLOCKYWORLD_H
#define BLOCKYWORLD_H
#include <CImg.h>
namespace logic {
class BlockyWorld {
public:
BlockyWorld( const CImg<float>* heightmap );
};
}
#endif // BLOCKYWORLD_H
BlockyWorld.cpp
#include "BlockyWorld.hpp"
namespace logic {
BlockyWorld::BlockyWorld( const CImg<float>* heightmap ) {}
}
main.cpp
#include <CImg.h>
#include "logic/BlockyWorld.hpp"
//...
CImg<float> heigthMap;
logic::BlockyWorld world( &heigthMap );
//...
I get alot of errors while compiling:
main.cpp:
include\logic\blockyworld.hpp(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
include\logic\blockyworld.hpp(9): error C2143: syntax error : missing ',' before '<'
main.cpp(85): error C2664: 'logic::BlockyWorld::BlockyWorld(const logic::BlockyWorld &)' : cannot convert argument 1 from 'cimg_library::CImg<float>' to 'const int'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
BlockyWorld.hpp & cpp
include\logic\blockyworld.hpp(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
include\logic\blockyworld.hpp(9): error C2143: syntax error : missing ',' before '<'
include\logic\blockyworld.cpp(4): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
include\logic\blockyworld.cpp(4): error C2143: syntax error : missing ',' before '<'
I don't think it's a circular inclusion error which sometimes causes these kinds of errors for me=).
I must be defining constructor wrong or maybe I'm defining implementation wrong? Was searching for an answer for abount an hour now so I would really use an explanation now.
And just to clarify - I'm not a beginner c/c++ programmer but these templates are confusing :(
Have a nice day and thank your for your answers.

CImg appears to be part of the cimg_library namespace.
Either add using namespace cimg_library to the top of your BlockyWorld.hpp file, or change the function signature to use the namespace like so:
BlockyWorld( const cimg_library::CImg<float>* heightmap );
Along with πάντα ῥεῖ's suggestion of matching up your pointer and reference types.

Related

Why is this code not compiling

I am trying to write a makeshift renderer for the tutorials on
this site. I have two classes SceneObject and RenderComponent. A SceneObject should contain a RenderComponent which should then draw the SceneObject. This is the code:
SceneObject.h
#ifndef _SCENE_OBJECT_H
#define _SCENE_OBJECT_H
#include <GLM/glm.hpp>
#include <GLM/gtc/matrix_transform.hpp>
#include <GLM/gtc/type_ptr.hpp>
#include <vector>
#include <iostream>
#include "..\headers\shader.h"
#include "..\headers\rendercomponent.h"
class SceneObject {
private:
glm::vec3 position;
float *vertices;
RenderComponent* renderComponent;
std::vector<Shader> shaders;
public:
SceneObject(glm::vec3, GLfloat*);
~SceneObject();
bool init();
RenderComponent& getRenderComponent() const;
};
#endif
SceneObject.cpp
#include "..\headers\sceneobject.h"
SceneObject::SceneObject(glm::vec3 pos, GLfloat* objectVertices) {
this->position = pos;
this->vertices = objectVertices;
renderComponent = new RenderComponent(*this);
}
SceneObject::~SceneObject() {}
RenderComponent& SceneObject::getRenderComponent() const {
return *renderComponent;
}
bool SceneObject::init() {
if (!renderComponent->initialize()) {
return false;
}
}
RenderComponent.h
#ifndef _RENDER_COMPONENT_H
#define _RENDER_COMPONENT_H
#include <GLM/glm.hpp>
#include <GLM/gtc/matrix_transform.hpp>
#include <GLM/gtc/type_ptr.hpp>
#include <iostream>
#include "../headers/sceneobject.h"
class RenderComponent {
SceneObject &sceneObject;
public:
RenderComponent(SceneObject&);
RenderComponent(const RenderComponent&);
bool initialize();
void draw();
SceneObject& getSceneObject() const;
};
#endif
RenderComponent.cpp
#include "..\headers\rendercomponent.h"
RenderComponent::RenderComponent(SceneObject& obj)
:sceneObject(obj){}
RenderComponent::RenderComponent(const RenderComponent& ref)
: sceneObject(ref.getSceneObject()){}
bool RenderComponent::initialize() {}
void RenderComponent::draw() {}
SceneObject& RenderComponent::getSceneObject() const {
return sceneObject;
}
The following errors are produced.
Error C2143 syntax error: missing ';' before '&'
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int
Error C2238 unexpected token(s) preceding ';' syntax error: identifier 'SceneObject'
Error C2143 syntax error: missing ';' before '&'
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int
Error C2238 unexpected token(s) preceding ';'
Error C2664 'RenderComponent::RenderComponent(const RenderComponent &)': cannot convert argument 1 from 'SceneObject' to 'const RenderComponent &'
Error C2143 syntax error: missing ';' before '&'
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int
Error C2238 unexpected token(s) preceding ';'
Error C2061 syntax error: identifier 'SceneObject'
Error C2143 syntax error: missing ';' before '&'
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int
Error C2238 unexpected token(s) preceding ';'
Error C2143 syntax error: missing ';' before '*'
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int
Error C2238 unexpected token(s) preceding ';'
Error C2143 syntax error: missing ';' before '&'
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int
Error C2238 unexpected token(s) preceding ';'
From what i understand, the compiler is saying that SceneObject is not a type. I think this where the problem is. Any help is appreciated.
You have sceneobject.h including rendercomponent.h and rendercomponent.h including sceneobject.h. With the include guards one of them doesn't know about the classes defined in the other header.
Remove the include from one or both headers and just forward declare the class(es) instead.

C++ error runing "hello world", vs2010

#include <iostream>
using namespace std;
int main()
{
cout<<"hello world!"<<endl;
return 0;
}
I am trying to run this simplest code in vs2010, but I received more than 100 errors when I was compiling it.
f:\vs2010\vc\include\fstream(14): error C2143: syntax error : missing ';' before '*'
f:\vs2010\vc\include\fstream(14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
f:\vs2010\vc\include\fstream(16): error C2653: 'ios_base' : is not a class or namespace name
f:\vs2010\vc\include\fstream(16): error C2061: syntax error : identifier 'openmode'
f:\vs2010\vc\include\fstream(16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Errors like these. What should I do?
try to include this lab in your code
#include "stdafx.h"

Why C++ not know TIMESTAMP_STRUCT type?

I use Visual Studio 2005
When I compile, I get this error:
Error 1 error C2146: syntax error : missing ';' before identifier 'mDropEndTime'
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
struct MB_SYN_DROPITEM_UPDATE : MSGBUF
{
long mCid; // Index
unsigned long mItemIdx; // idx
TIMESTAMP_STRUCT mDropEndTime; // This is error line
};
Why doesn't C++ know TIMESTAMP_STRUCT?
TIMESTAMP_STRUCT is something defined in sqlext.h
You must add
#include <sqlext.h>
Because TIMESTAMP_STRUCT is not part of the C++ standard.

getting errors while instantiating pointer for structs of native c++ code from c++/cli

below mentioned is my c++/cli "*.h" file
// rmsCInterfaceWrapper.h
#ifndef RMSCINTERFACE_H
#define RMSCINTERFACE_H
#ifndef RMSREQINFO_H
#define RMSREQINFO_H
#ifndef RMSCLIENTINFO_H
#define RMSCLIENTINFO_H
#ifndef RMSPHYSICIANINFO_H
#define RMSPHYSICIANINFO_H
#ifndef RMSREQPOLICYINFO_H
#define RMSREQPOLICYINFO_H
#pragma once
#include "D:\nbsource code\RMS\rmsCAPI\rmsCInterface.h"
#pragma comment(lib,"rmsCAPI.lib")
#include "D:\nbsource code\RMS\rmsDLL\rmsReqInfo.h"
#include "D:\nbsource code\RMS\rmsDLL\rmsClientInfo.h"
#include "D:\nbsource code\RMS\rmsDLL\rmsPhysicianInfo.h"
#include "D:\nbsource code\RMS\rmsDLL\rmsReqPolicyInfo.h"
#pragma comment(lib,"rmsDLL.lib")
using namespace System;
namespace rmsCInterfaceWrapper
{
public ref class rmsCInterface
{
// TODO: Add your methods for this class here.
private:
rmsReqInfoStruct *rmsReqInfo;
rmsClientInfoStruct *rmsClientInfo;
rmsPhysicianInfoStruct *rmsPhysicianInfo;
rmsReqPolicyInfoStruct *rmsReqPolInfo;
rmsAddlOrderInfoStruct *rmsAddOrderInfo;
public:
rmsCInterface();
~rmsCInterface();
long OrderReq();
};
}
#endif
i have declared pointer for these native c++ structures rmsReqInfoStruct, rmsClientInfoStruct, rmsPhysicianInfoStruct, rmsReqPolicyInfoStruct, rmsAddlOrderInfoStruct. wheni compile this i am getting these below mentioned errors and i am not sure why i am getting those
1>d:\vamsi\rmscinterfacewrapper\rmscinterfacewrapper\rmsCInterfaceWrapper.h(30) : error C2143: syntax error : missing ';' before '*'
1>d:\vamsi\rmscinterfacewrapper\rmscinterfacewrapper\rmsCInterfaceWrapper.h(30) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\vamsi\rmscinterfacewrapper\rmscinterfacewrapper\rmsCInterfaceWrapper.h(30) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\vamsi\rmscinterfacewrapper\rmscinterfacewrapper\rmsCInterfaceWrapper.h(31) : error C2143: syntax error : missing ';' before '*'
1>d:\vamsi\rmscinterfacewrapper\rmscinterfacewrapper\rmsCInterfaceWrapper.h(31) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\vamsi\rmscinterfacewrapper\rmscinterfacewrapper\rmsCInterfaceWrapper.h(31) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\vamsi\rmscinterfacewrapper\rmscinterfacewrapper\rmsCInterfaceWrapper.h(32) : error C2143: syntax error : missing ';' before '*'
1>d:\vamsi\rmscinterfacewrapper\rmscinterfacewrapper\rmsCInterfaceWrapper.h(32) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\vamsi\rmscinterfacewrapper\rmscinterfacewrapper\rmsCInterfaceWrapper.h(32) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\vamsi\rmscinterfacewrapper\rmscinterfacewrapper\rmsCInterfaceWrapper.h(33) : error C2143: syntax error : missing ';' before '*'
1>d:\vamsi\rmscinterfacewrapper\rmscinterfacewrapper\rmsCInterfaceWrapper.h(33) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\vamsi\rmscinterfacewrapper\rmscinterfacewrapper\rmsCInterfaceWrapper.h(33) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\vamsi\rmscinterfacewrapper\rmscinterfacewrapper\rmsCInterfaceWrapper.h(34) : error C2143: syntax error : missing ';' before '*'
1>d:\vamsi\rmscinterfacewrapper\rmscinterfacewrapper\rmsCInterfaceWrapper.h(34) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\vamsi\rmscinterfacewrapper\rmscinterfacewrapper\rmsCInterfaceWrapper.h(34) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
All these errors are showing up at the structure pointers in the class private declaration itself and i am unable to figure them out why they are causing. can any one please help.
I was able to solve the issue. As i have created a clr--->class library project it automatically generated the stdafx.h and stdafx.cpp files. As i am writing a wrapper around my native c++ code using c++/cli i copied the auto generated c++/cli stdafx.h file with my native stdafx.h file and removed the #ifndef,#define,#pragma statements from the .h file that i have provided above, so doing all these it worked, and i was able to generate the dll file which i can use in my c# program as a reference... Thank you every one for your help...

How to use circularly dependent classes in C++?

This code is not compiling.
What modification can I do to achieve the desired result?
ClassOne.h
#ifndef _CLASS_ONE_
#define _CLASS_ONE_
#include <string>
#include "ClassTwo.h"
class ClassTwo;
class ClassOne
{
private:
string message;
friend ClassTwo;
ClassTwo m_ClassTwo;
public:
ClassOne();
void Display();
};
#endif
ClassTwo.h
#ifndef _CLASS_TWO_
#define _CLASS_TWO_
#include <string>
#include "ClassOne.h"
class ClassOne;
class ClassTwo
{
private:
string message;
friend ClassOne;
ClassOne m_ClassOne;
public:
ClassTwo();
void Display();
};
#endif
ClassOne.cpp
#include "ClassOne.h"
#include "ClassTwo.h"
#include <iostream>
ClassOne :: ClassOne()
{
std::cout<<"ClassOne()...called\n";
this->m_ClassTwo.message = "ClassOne - Message\n";
}
void ClassOne :: Display()
{
std::cout<<this->m_ClassTwo.message;
}
ClassTwo.cpp
#include "ClassTwo.h"
#include "ClassOne.h"
#include <iostream>
ClassTwo :: ClassTwo()
{
std::cout<<"ClassTwo()...called\n";
this->m_ClassOne.message = "ClassTwo - Message\n";
}
void ClassTwo :: Display()
{
std::cout<<this->m_ClassOne.message;
}
main.cpp
#include "ClassOne.h"
#include "ClassTwo.h"
int main()
{
ClassOne one;
one.Display();
ClassTwo two;
two.Display();
}
Error messages
1 error C2146: syntax error : missing ';' before identifier 'message'
2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
4 error C2079: 'ClassTwo::m_ClassOne' uses undefined class 'ClassOne'
5 error C2146: syntax error : missing ';' before identifier 'message'
6 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
7 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
8 error C2039: 'message' : is not a member of 'ClassTwo'
9 error C2039: 'message' : is not a member of 'ClassTwo'
10 error C2146: syntax error : missing ';' before identifier 'message'
11 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
12 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
13 error C2079: 'ClassOne::m_ClassTwo' uses undefined class 'ClassTwo'
14 error C2146: syntax error : missing ';' before identifier 'message'
15 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
16 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
17 error C2039: 'message' : is not a member of 'ClassOne'
18 error C2039: 'message' : is not a member of 'ClassOne'
19 error C2146: syntax error : missing ';' before identifier 'message'
20 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
21 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
22 error C2079: 'ClassTwo::m_ClassOne' uses undefined class 'ClassOne'
23 error C2146: syntax error : missing ';' before identifier 'message'
24 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
25 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
You cannot, ever, compile that code. You have described a system where type A contains type B, and type B contains type A. Therefore, both type A and B recursively and infinitely contain themselves, which is impossible. You must fundamentally re-architect your code to eliminate this problem.
Also, your friend syntax is wrong.
As said, you need to forward declare at least one of ClassOne or ClassTwo.
You ClassOne.h could therefor look like:
#ifndef _CLASS_ONE_
#define _CLASS_ONE_
#include <string>
class ClassTwo;
class ClassOne
{
private:
string message;
ClassTwo* m_ClassTwo;
public:
ClassOne();
void Display();
};
#endif
As you can see, we declare ClassTwo, but do not include it. We basically only tell the compiler that yes, we do have a ClassTwo, but we don't really care what it contains right now.
Also look at your ClassTwo member, this is now a pointer. The reason is that a member would require the compiler to know the size of the object, which we currently have no clue what is. Therefor you need either a pointer or a ref.
Next, in your ClassOne.cpp, would will need to include ClassTwo.h, to get the functions and size of that class.
A few things though:
With a forward declaration you can not inherit from ClassTwo, use the methods of the forwarded class in the header file (How would the compiler know which methods exists?) Define functions or methods using the forwarded class, that is you have to pass by reference or pass a pointer.
1) Use forward declarations:
add
class ClassTwo;
to ClassOne.h
and
class ClassTwo;
to ClassTwo.h
2) You need to create your member variables (or at least one of them) dynamically, using operator new, or if you wish, with one of the smart-pointers, like boost::shared_ptr from boost library or std::shared_ptr from standard library if you use C++11.