I am confused on how to separate implementation and declarations code of a simple class into a new header and cpp file. For example, how would I separate the code for the following class?
class A2DD
{
private:
int gx;
int gy;
public:
A2DD(int x,int y)
{
gx = x;
gy = y;
}
int getSum()
{
return gx + gy;
}
};
The class declaration goes into the header file. It is important that you add the #ifndef include guards. Most compilers now also support #pragma once. Also I have omitted the private, by default C++ class members are private.
// A2DD.h
#ifndef A2DD_H
#define A2DD_H
class A2DD
{
int gx;
int gy;
public:
A2DD(int x,int y);
int getSum();
};
#endif
and the implementation goes in the CPP file:
// A2DD.cpp
#include "A2DD.h"
A2DD::A2DD(int x,int y)
{
gx = x;
gy = y;
}
int A2DD::getSum()
{
return gx + gy;
}
In general your .h contains the class defition, which is all your data and all your method declarations. Like this in your case:
A2DD.h:
class A2DD
{
private:
int gx;
int gy;
public:
A2DD(int x,int y);
int getSum();
};
And then your .cpp contains the implementations of the methods like this:
A2DD.cpp:
A2DD::A2DD(int x,int y)
{
gx = x;
gy = y;
}
int A2DD::getSum()
{
return gx + gy;
}
It's important to point out to readers stumbling upon this question when researching the subject in a broader fashion that the accepted answer's procedure is not required in the case you just want to split your project into files. It's only needed when you need multiple implementations of single classes. If your implementation per class is one, just one header file for each is enough.
Hence, from the accepted answer's example only this part is needed:
#ifndef MYHEADER_H
#define MYHEADER_H
//Class goes here, full declaration AND implementation
#endif
The #ifndef etc. preprocessor definitions allow it to be used multiple times.
PS. The topic becomes clearer once you realize C/C++ is 'dumb' and #include is merely a way to say "dump this text at this spot".
Basically a modified syntax of function declaration/definitions:
a2dd.h
class A2DD
{
private:
int gx;
int gy;
public:
A2DD(int x,int y);
int getSum();
};
a2dd.cpp
A2DD::A2DD(int x,int y)
{
gx = x;
gy = y;
}
int A2DD::getSum()
{
return gx + gy;
}
A2DD.h
class A2DD
{
private:
int gx;
int gy;
public:
A2DD(int x,int y);
int getSum();
};
A2DD.cpp
A2DD::A2DD(int x,int y)
{
gx = x;
gy = y;
}
int A2DD::getSum()
{
return gx + gy;
}
The idea is to keep all function signatures and members in the header file.
This will allow other project files to see how the class looks like without having to know the implementation.
And besides that, you can then include other header files in the implementation instead of the header. This is important because whichever headers are included in your header file will be included (inherited) in any other file that includes your header file.
You leave the declarations in the header file:
class A2DD
{
private:
int gx;
int gy;
public:
A2DD(int x,int y); // leave the declarations here
int getSum();
};
And put the definitions in the implementation file.
A2DD::A2DD(int x,int y) // prefix the definitions with the class name
{
gx = x;
gy = y;
}
int A2DD::getSum()
{
return gx + gy;
}
You could mix the two (leave getSum() definition in the header for instance). This is useful since it gives the compiler a better chance at inlining for example. But it also means that changing the implementation (if left in the header) could trigger a rebuild of all the other files that include the header.
Note that for templates, you need to keep it all in the headers.
Usually you put only declarations and really short inline functions in the header file:
For instance:
class A {
public:
A(); // only declaration in the .h unless only a short initialization list is used.
inline int GetA() const {
return a_;
}
void DoSomethingCoplex(); // only declaration
private:
int a_;
};
I won't refer too your example as it is quite simple for a general answer (for example it doesn't contain templated functions ,which force you to implement them on the header) , what I follow as a rule of thumb is the pimpl idiom
It has quite some benefits as you get faster compilation times and the syntactic sugar :
class->member instead of class.member
The only drawback is the extra pointer you pay.
Related
I am confused on how to separate implementation and declarations code of a simple class into a new header and cpp file. For example, how would I separate the code for the following class?
class A2DD
{
private:
int gx;
int gy;
public:
A2DD(int x,int y)
{
gx = x;
gy = y;
}
int getSum()
{
return gx + gy;
}
};
The class declaration goes into the header file. It is important that you add the #ifndef include guards. Most compilers now also support #pragma once. Also I have omitted the private, by default C++ class members are private.
// A2DD.h
#ifndef A2DD_H
#define A2DD_H
class A2DD
{
int gx;
int gy;
public:
A2DD(int x,int y);
int getSum();
};
#endif
and the implementation goes in the CPP file:
// A2DD.cpp
#include "A2DD.h"
A2DD::A2DD(int x,int y)
{
gx = x;
gy = y;
}
int A2DD::getSum()
{
return gx + gy;
}
In general your .h contains the class defition, which is all your data and all your method declarations. Like this in your case:
A2DD.h:
class A2DD
{
private:
int gx;
int gy;
public:
A2DD(int x,int y);
int getSum();
};
And then your .cpp contains the implementations of the methods like this:
A2DD.cpp:
A2DD::A2DD(int x,int y)
{
gx = x;
gy = y;
}
int A2DD::getSum()
{
return gx + gy;
}
It's important to point out to readers stumbling upon this question when researching the subject in a broader fashion that the accepted answer's procedure is not required in the case you just want to split your project into files. It's only needed when you need multiple implementations of single classes. If your implementation per class is one, just one header file for each is enough.
Hence, from the accepted answer's example only this part is needed:
#ifndef MYHEADER_H
#define MYHEADER_H
//Class goes here, full declaration AND implementation
#endif
The #ifndef etc. preprocessor definitions allow it to be used multiple times.
PS. The topic becomes clearer once you realize C/C++ is 'dumb' and #include is merely a way to say "dump this text at this spot".
Basically a modified syntax of function declaration/definitions:
a2dd.h
class A2DD
{
private:
int gx;
int gy;
public:
A2DD(int x,int y);
int getSum();
};
a2dd.cpp
A2DD::A2DD(int x,int y)
{
gx = x;
gy = y;
}
int A2DD::getSum()
{
return gx + gy;
}
A2DD.h
class A2DD
{
private:
int gx;
int gy;
public:
A2DD(int x,int y);
int getSum();
};
A2DD.cpp
A2DD::A2DD(int x,int y)
{
gx = x;
gy = y;
}
int A2DD::getSum()
{
return gx + gy;
}
The idea is to keep all function signatures and members in the header file.
This will allow other project files to see how the class looks like without having to know the implementation.
And besides that, you can then include other header files in the implementation instead of the header. This is important because whichever headers are included in your header file will be included (inherited) in any other file that includes your header file.
You leave the declarations in the header file:
class A2DD
{
private:
int gx;
int gy;
public:
A2DD(int x,int y); // leave the declarations here
int getSum();
};
And put the definitions in the implementation file.
A2DD::A2DD(int x,int y) // prefix the definitions with the class name
{
gx = x;
gy = y;
}
int A2DD::getSum()
{
return gx + gy;
}
You could mix the two (leave getSum() definition in the header for instance). This is useful since it gives the compiler a better chance at inlining for example. But it also means that changing the implementation (if left in the header) could trigger a rebuild of all the other files that include the header.
Note that for templates, you need to keep it all in the headers.
Usually you put only declarations and really short inline functions in the header file:
For instance:
class A {
public:
A(); // only declaration in the .h unless only a short initialization list is used.
inline int GetA() const {
return a_;
}
void DoSomethingCoplex(); // only declaration
private:
int a_;
};
I won't refer too your example as it is quite simple for a general answer (for example it doesn't contain templated functions ,which force you to implement them on the header) , what I follow as a rule of thumb is the pimpl idiom
It has quite some benefits as you get faster compilation times and the syntactic sugar :
class->member instead of class.member
The only drawback is the extra pointer you pay.
I am confused on how to separate implementation and declarations code of a simple class into a new header and cpp file. For example, how would I separate the code for the following class?
class A2DD
{
private:
int gx;
int gy;
public:
A2DD(int x,int y)
{
gx = x;
gy = y;
}
int getSum()
{
return gx + gy;
}
};
The class declaration goes into the header file. It is important that you add the #ifndef include guards. Most compilers now also support #pragma once. Also I have omitted the private, by default C++ class members are private.
// A2DD.h
#ifndef A2DD_H
#define A2DD_H
class A2DD
{
int gx;
int gy;
public:
A2DD(int x,int y);
int getSum();
};
#endif
and the implementation goes in the CPP file:
// A2DD.cpp
#include "A2DD.h"
A2DD::A2DD(int x,int y)
{
gx = x;
gy = y;
}
int A2DD::getSum()
{
return gx + gy;
}
In general your .h contains the class defition, which is all your data and all your method declarations. Like this in your case:
A2DD.h:
class A2DD
{
private:
int gx;
int gy;
public:
A2DD(int x,int y);
int getSum();
};
And then your .cpp contains the implementations of the methods like this:
A2DD.cpp:
A2DD::A2DD(int x,int y)
{
gx = x;
gy = y;
}
int A2DD::getSum()
{
return gx + gy;
}
It's important to point out to readers stumbling upon this question when researching the subject in a broader fashion that the accepted answer's procedure is not required in the case you just want to split your project into files. It's only needed when you need multiple implementations of single classes. If your implementation per class is one, just one header file for each is enough.
Hence, from the accepted answer's example only this part is needed:
#ifndef MYHEADER_H
#define MYHEADER_H
//Class goes here, full declaration AND implementation
#endif
The #ifndef etc. preprocessor definitions allow it to be used multiple times.
PS. The topic becomes clearer once you realize C/C++ is 'dumb' and #include is merely a way to say "dump this text at this spot".
Basically a modified syntax of function declaration/definitions:
a2dd.h
class A2DD
{
private:
int gx;
int gy;
public:
A2DD(int x,int y);
int getSum();
};
a2dd.cpp
A2DD::A2DD(int x,int y)
{
gx = x;
gy = y;
}
int A2DD::getSum()
{
return gx + gy;
}
A2DD.h
class A2DD
{
private:
int gx;
int gy;
public:
A2DD(int x,int y);
int getSum();
};
A2DD.cpp
A2DD::A2DD(int x,int y)
{
gx = x;
gy = y;
}
int A2DD::getSum()
{
return gx + gy;
}
The idea is to keep all function signatures and members in the header file.
This will allow other project files to see how the class looks like without having to know the implementation.
And besides that, you can then include other header files in the implementation instead of the header. This is important because whichever headers are included in your header file will be included (inherited) in any other file that includes your header file.
You leave the declarations in the header file:
class A2DD
{
private:
int gx;
int gy;
public:
A2DD(int x,int y); // leave the declarations here
int getSum();
};
And put the definitions in the implementation file.
A2DD::A2DD(int x,int y) // prefix the definitions with the class name
{
gx = x;
gy = y;
}
int A2DD::getSum()
{
return gx + gy;
}
You could mix the two (leave getSum() definition in the header for instance). This is useful since it gives the compiler a better chance at inlining for example. But it also means that changing the implementation (if left in the header) could trigger a rebuild of all the other files that include the header.
Note that for templates, you need to keep it all in the headers.
Usually you put only declarations and really short inline functions in the header file:
For instance:
class A {
public:
A(); // only declaration in the .h unless only a short initialization list is used.
inline int GetA() const {
return a_;
}
void DoSomethingCoplex(); // only declaration
private:
int a_;
};
I won't refer too your example as it is quite simple for a general answer (for example it doesn't contain templated functions ,which force you to implement them on the header) , what I follow as a rule of thumb is the pimpl idiom
It has quite some benefits as you get faster compilation times and the syntactic sugar :
class->member instead of class.member
The only drawback is the extra pointer you pay.
I am confused on how to separate implementation and declarations code of a simple class into a new header and cpp file. For example, how would I separate the code for the following class?
class A2DD
{
private:
int gx;
int gy;
public:
A2DD(int x,int y)
{
gx = x;
gy = y;
}
int getSum()
{
return gx + gy;
}
};
The class declaration goes into the header file. It is important that you add the #ifndef include guards. Most compilers now also support #pragma once. Also I have omitted the private, by default C++ class members are private.
// A2DD.h
#ifndef A2DD_H
#define A2DD_H
class A2DD
{
int gx;
int gy;
public:
A2DD(int x,int y);
int getSum();
};
#endif
and the implementation goes in the CPP file:
// A2DD.cpp
#include "A2DD.h"
A2DD::A2DD(int x,int y)
{
gx = x;
gy = y;
}
int A2DD::getSum()
{
return gx + gy;
}
In general your .h contains the class defition, which is all your data and all your method declarations. Like this in your case:
A2DD.h:
class A2DD
{
private:
int gx;
int gy;
public:
A2DD(int x,int y);
int getSum();
};
And then your .cpp contains the implementations of the methods like this:
A2DD.cpp:
A2DD::A2DD(int x,int y)
{
gx = x;
gy = y;
}
int A2DD::getSum()
{
return gx + gy;
}
It's important to point out to readers stumbling upon this question when researching the subject in a broader fashion that the accepted answer's procedure is not required in the case you just want to split your project into files. It's only needed when you need multiple implementations of single classes. If your implementation per class is one, just one header file for each is enough.
Hence, from the accepted answer's example only this part is needed:
#ifndef MYHEADER_H
#define MYHEADER_H
//Class goes here, full declaration AND implementation
#endif
The #ifndef etc. preprocessor definitions allow it to be used multiple times.
PS. The topic becomes clearer once you realize C/C++ is 'dumb' and #include is merely a way to say "dump this text at this spot".
Basically a modified syntax of function declaration/definitions:
a2dd.h
class A2DD
{
private:
int gx;
int gy;
public:
A2DD(int x,int y);
int getSum();
};
a2dd.cpp
A2DD::A2DD(int x,int y)
{
gx = x;
gy = y;
}
int A2DD::getSum()
{
return gx + gy;
}
A2DD.h
class A2DD
{
private:
int gx;
int gy;
public:
A2DD(int x,int y);
int getSum();
};
A2DD.cpp
A2DD::A2DD(int x,int y)
{
gx = x;
gy = y;
}
int A2DD::getSum()
{
return gx + gy;
}
The idea is to keep all function signatures and members in the header file.
This will allow other project files to see how the class looks like without having to know the implementation.
And besides that, you can then include other header files in the implementation instead of the header. This is important because whichever headers are included in your header file will be included (inherited) in any other file that includes your header file.
You leave the declarations in the header file:
class A2DD
{
private:
int gx;
int gy;
public:
A2DD(int x,int y); // leave the declarations here
int getSum();
};
And put the definitions in the implementation file.
A2DD::A2DD(int x,int y) // prefix the definitions with the class name
{
gx = x;
gy = y;
}
int A2DD::getSum()
{
return gx + gy;
}
You could mix the two (leave getSum() definition in the header for instance). This is useful since it gives the compiler a better chance at inlining for example. But it also means that changing the implementation (if left in the header) could trigger a rebuild of all the other files that include the header.
Note that for templates, you need to keep it all in the headers.
Usually you put only declarations and really short inline functions in the header file:
For instance:
class A {
public:
A(); // only declaration in the .h unless only a short initialization list is used.
inline int GetA() const {
return a_;
}
void DoSomethingCoplex(); // only declaration
private:
int a_;
};
I won't refer too your example as it is quite simple for a general answer (for example it doesn't contain templated functions ,which force you to implement them on the header) , what I follow as a rule of thumb is the pimpl idiom
It has quite some benefits as you get faster compilation times and the syntactic sugar :
class->member instead of class.member
The only drawback is the extra pointer you pay.
I am following a tutorial to create a wrapper around C++ code, so that it can be called from C#.
I get an error compiling the wrapper though.
Header.h
class MyClass{
public:
MyClass(int x, int y);
double GetSum();
private:
int x_;
int y_;
};
Below is the source file (body.cpp)
#include "Header.h"
MyClass::MyClass(int x, int y)
{
x = 8;
y = 8;
}
double MyClass::GetSum()
{
int r = x_ + y_;
return r;
}
The wrapper class/dll is as below
#include "C:\Users\tumelo\Documents\Visual Studio 2012\Projects\Emgu\MyClassCpp\MyClassCpp\Header.h"
#include "C:\Users\tumelo\Documents\Visual Studio 2012\Projects\Emgu\MyClassCpp\MyClassCpp\Body.cpp"
//for the function you want made avaible from the dll
extern "C" __declspec(dllexport) double GetResults(int x, int y)
{
//create an instance of the class
MyClass myClass(int x, int y);
return myClass.GetSum();
}
I get an in the wrapper class right at the return statement. The class method does not seem to be recognised for some reason. The error reads:
error C2228: left of '.GetSum' must have class/struct/union
What puzzles me is that this is copy and paste from the tutorial but mine does not work. What could I be missing?
You meant:
MyClass myClass(x, y);
instead of
MyClass myClass(int x, int y);
What you typed is declaring a function named "myClass" that returns a "MyClass" instance and takes two integers. You meant to instantiate a variable named "myClass" of type "MyClass" by by passing it x and y.
I have been attempting to work with classes in c++ for the first time. My circle class and associated header file were working fine, I then moved some files and since then keep getting an error which i have displayed below.
c:\circleobje.cpp(3): error C2011: 'CircleObje' : 'class' type redefinition
c:\circleobje.h(4) : see declaration of 'CircleObje'
CircleObje.h
#ifndef CircleObje_H
#define CircleObje_H
class CircleObje
{
public:
void setCol(float r, float g, float b);
void setCoord(int x, int y);
float getR();
float getG();
float getB();
int getX();
int getY();
};
#endif
CircleObje.cpp
#include "CircleObje.h"
class CircleObje {
float rVal, gVal, bVal;
int xCor, yCor;
public:
void setCol(float r, float g, float b)
{
rVal = r;
gVal = g;
bVal = b;
}
void setCoord(int x, int y)
{
xCor = x;
yCor = y;
}
...
};
I haven't copied all of the .cpp functions as I didn't think they were relevant. These files were working without issue before I moved the file locations. Even after renaming them I still have the same error as above. Any ideas to solve the problem?
The issue is that you are defining the class twice just as the compiler is telling you. In the cpp you should provide the definitions of the functions like so:
MyClass::MyClass() {
//my constructor
}
or
void MyClass::foo() {
//foos implementation
}
so your cpp should look like:
void CirleObje::setCol(float r, float g, float b)
{
rVal = r;
gVal = g;
bVal = b;
}
void CircleObje::setCoord(int x, int y)
{
xCor = x;
yCor = y;
}
...
And all the class variables should be defined in the .h file inside of your class.
You have defined the class twice, in the header and in the cpp, so in the .cpp the compiler sees two definitions. Remove the definition of the class on the .cpp.
Class functions should be implemented in the cpp in this way:
<return_type> <class_name>::<function_name>(<function_parameters>)
{
...
}
Consider this example class:
//foo.hpp
struct foo
{
int a;
void f();
}
The class is implemented in the foo.cpp file:
#include "foo.hpp"
void foo::f()
{
//Do something...
}
you are declaring your class multiple times once in header file and another in .cpp file which is redefining your class.
CircleObje.h
#ifndef CircleObje_H
#define CircleObje_H
class CircleObje
{
public:
void setCol(float r, float g, float b);
void setCoord(int x, int y);
float getR();
float getG();
float getB();
int getX();
int getY();
public:
float rVal, gVal, bVal;
int xCor, yCor;
};
#endif
CircleObje.cpp
#include "CircleObje.h"
void CircleObje::void setCol(float r, float g, float b)
{
rVal = r;
gVal = g;
bVal = b;
}
void CircleObje::setCoord(int x, int y)
{
xCor = x;
yCor = y;
}
Remove class CircleObje {, public and the ending bracket }; and it should work. You already defined your class in the .H, thus no need to redefine it in the CPP.
Also, you should write your member implementation (in CPP file) like this :
float CircleObje::getR() { /* your code */ }
you need to put #pragma once in first line of header file then the errors will be disappears