I know how to define a class method inside of the same file with which the class is in.
For example:
class Robot
{public:
int location;
void Moves(); //Class method to be defined
}
void Robot::Moves()
{//Method definition here }
I do NOT know hot to define a class outside of the file in which the class is in. I tried creating a .hpp file and defining a class method inside of it, but my compiler signified that it could not load a class definition from a file other than the one the class was created in, or it would be as though I was placing a function's definition before the include directives.
Please note: The original class file is also in a .hpp file, as I have yet to learn how to use .cpp files other than the main one.
This is done using C++/Win32.
Create a .cpp file along these guidelines
Include your class header file in your .cpp file
Include all necessary headers you would use in main.cpp
use the scope operator for your class
#include <iostream>
#include "foo.hpp"
foo::foo()
{
// code here
}
void foo::OtherFunc()
{
// other stuff here
}
Just put your definition in a .cpp file and link it with your application.
Robot.hpp:
class Robot
{public:
int location;
void Moves(); //Class method to be defined
}
Robot.cpp:
#include "Robot.hpp"
void Robot::Moves()
{//Method definition here }
Related
I have a question in regards to building a project in C++ with abstract classes.
Suppose you have an abstract class A in a header file and then you have class B in a cpp file to create an object from class A. So far we have:
Class_A.h, Class_B.cpp
Within the Class_B.cpp file, I have included the header Class_A.h.
I want to now create objects of Class_B in a main file, for example: main.cpp. I added the header Class_A.h in main.cpp and when compiled, I get 'Class_B’ was not declared in this scope as an error. All files are in the same directory. I'm not too sure how to fix this or whether or not I am not using abstract classes correctly. All the examples I found online did something along the lines of: defining Class_B inside main.cpp without creating a separate cpp file for Class_B.
Example:
Abstract class: Car.h
Instance of abstract class: Toyota.cpp
main.cpp:
#include 'Car.h'
...
Toyota new_car = new Toyota(...);
Just need to organize your code. There's no hard and fast rules about how that needs to be done, but generally you define classes in header files (.h or .hpp) and implement the class in source files (.cpp). In this situation I would:
Class_A.h
class Class_A
{
public:
virtual void myFunc() = 0; // Class_A is abstract
...
}
Class_B.h
#include "Class_A.h"
class Class_B : public Class_A
{
public:
void myFunc();
...
}
Class_B.cpp
Class_B::myFunc()
{
...
}
main.cpp
#include "Class_B.h"
int main()
{
Class_B myClassB;
myClassB.myFunc();
...
return 0;
}
And of course you need to feed all the source files to your compiler correctly, at least Class_B.cpp and main.cpp in this case.
In one of my classes header file Lfo.h, I have a class definition where I put the member function definition out of the class (It might be better to have a separate .cpp file but it should be ok put here?):
// Lfo.h
class CLfo
{
public:
static int create (CLfo*& pCLfo);
};
int CLfo::create(CLfo *&pCLfo)
{
pCLfo = new CLfo;
return 0;
}
Then I have another class called CVibrato:
// Vibrato.h
class CVibrato
{
public:
static int create (CVibrato*& pCVibrato);
private:
CVibrato();
};
and the .cpp file (in the cpp file, I include Lfo.h because later on the vibrato class will have a lfo member but I haven't implemented right now):
// Vibrato.cpp
#include "Lfo.h"
#include "Vibrato.h"
int CVibrato::create(CVibrato *&pCVibrato)
{
pCVibrato = new CVibrato();
return 0;
}
CVibrato::CVibrato()
{
}
Then I want to create a instance of vibrato class in main()
#include "Vibrato.h"
#include "Lfo.h" // if comment this line out there will be no error, why is that?
int main()
{
CVibrato *vibrato = 0;
CVibrato::create(vibrato);
return 0;
}
However I get a 1 duplicate symbol for architecture x86_64 error. What is duplicated? It seems the reason is in Lfo.h, I put the definition of the member function outside of the class, if I put it inside, the program runs properly. But I cannot understand. In c++, aren't we allowed to do this? By the way, if one of my class (in my case vibrato) is going to have a class member of another class (in this case lfo), should I include the header file of member class in .h (vibrato.h) file or .cpp (vibrato.cpp) file?
Classes are declarations. No code is produced from a declaration. Even if you have a member function in the class, it is treated as if an inline by the compiler. Function bodies can be put in a header but should always be declared as inline. The compiler may not actually inline it, but it will treat it as a single instance for code creation.
Any time you:
void function( ) { }
Code is created for that function. If a header is included more than once the compiler is told to create the code more than once. But all functions must have unique names! So you get the duplicate error. That is why code generating lines belong in the .cpp files.
'inline' tells the compiler not to create immediate code but to create the code at the usage point.
You can't put class method definition directly in a header file, unless you explicitly mark it as inline. Like the following:
// Lfo.h
class CLfo
{
public:
inline static int create (CLfo*& pCLfo);
};
int CLfo::create(CLfo *&pCLfo)
{
pCLfo = new CLfo;
return 0;
}
Or,
// Lfo.h
class CLfo
{
public:
static int create (CLfo*& pCLfo);
};
inline int CLfo::create(CLfo *&pCLfo)
{
pCLfo = new CLfo;
return 0;
}
In below code while compiling i am getting error ,
Error :
main.cpp:8:57: error: expected type-specifier before ‘Staircase’
std::unique_ptr algodiagnostic (new Staircase());
compilation command
g++ HardwareDiagnostic.cpp HardwareDiagnostic.h main.cpp -std=c++0x -o res
it works fine if compile entire code in single file without creating header file and main.cpp separately. Can anybody suggest how to fix this issue.
//HardwareDiagnostic.h//
#ifndef HARDWAREDIAGNOSTIC_H_
#define HARDWAREDIAGNOSTIC_H_
#include <iostream>
#include <memory>
using namespace std;
class HardwareDiagnostic
{
public:
virtual bool checkPort();
virtual void startDiagnostic();
virtual int publishAlgoDiagnosticInfo();
virtual void clearErrorStatus(){cout<<"Algorithm Diagnostics"<<endl;}
virtual ~HardwareDiagnostic() {cout<<"calling virtual destructor"<<endl;}
};
#endif /* HARDWAREDIAGNOSTIC_H_ */
//HardwareDiagnostic.cpp//
#include"HardwareDiagnostic.h"
class Localization : public HardwareDiagnostic
{
public:
Localization() { cout << "calling Localization constructor";}
bool checkPort(){cout<<"checkport :Localization";}
void startDiagnostic(){cout<<"start Diagnostic:Localization";}
int publishAlgoDiagnosticInfo() {cout<<"publish Diagnostic:Localization";}
void clearErrorStatus(){ cout<<"Localization:publish Diagnostic";}
~Localization () { cout<<"calling Localization destructor ";}
};
class Staircase : public HardwareDiagnostic
{
public:
Staircase () {cout<<"Staircase constructor";}
bool checkPort(){cout<<"Staircase";}
void startDiagnostic(){cout<<"StairCase:start Diagnostic";}
int publishAlgoDiagnosticInfo() {cout<<"StairCase:publish Diagnostic";}
void clearErrorStatus(){ cout<<"staircase:publish Diagnostic";}
~Staircase(){cout<<"calling Staircase destructor";}
};
//main.cpp//
#include "HardwareDiagnostic.h"
using namespace std;
int main() {
std::unique_ptr<HardwareDiagnostic> algodiagnostic (new Staircase());
return 0;
}
When compiler works on main.cpp, it sees Staricase usage, but doesn't see declaration for Staircase class, so error is raised. Your main.cpp file includes header, which describes HardwareDiagnostic class only, but no info for Staircase is provided.
Good practice is to keep class declarations in header files, rather than cpp files, so any other source can include header file and start using described class. It's OK to include definition for trivial class methods in header file as well (like getters/setters), but complicated methods should be declared in header file and defined in coresponding cpp file.
In your case I would do the following:
HardwareDiagnostic.h describes HardwareDiagnostic class. There is no using std line in this file, so anyone including this file won't implicitly start using undesired namespace. All methods are declared, but not defined.
HardwareDiagnostic.cpp defines methods of HardwareDiagnostic class. So there is using std line and definition of all methods.
Localization.h includes HardwareDiagnostic.h and describes Localization class. Having separate file for each class becomes very convenient as project grows.
Localization.cpp defines methods of Localization class.
Staircase.h and Staircase.cpp are built the same way as localization.
main.cpp includes HardwareDiagnostic.h and Staircase.h
I know this is a commonly asked question about C++ but following advice of other answers and such, I am still unable to get my seemingly simple code to work. My problem is the following code gives "error: redefinition of 'class Communicator'":
global.h
#ifndef GLOBAL_H
#define GLOBAL_H
class object_payload;
class pending_frame;
class Communicator {
private:
map<string,object_payload*> local_objects;
map<string,pending_frame*> remote_tasks;
bool listening;
public:
Communicator();
void stop_listening();
void add_to_remote_tasks(string name, pending_frame* pfr);
void listen();
void distributed_release(string task_name);
};
extern Communicator communicator;
#endif
global.cpp
#include "global.h"
class Communicator {
private:
map<string,object_payload*> local_objects;
map<string,pending_frame*> remote_tasks;
bool listening;
public:
Communicator(){
// implementation
}
void stop_listening(){
// implementation
}
void add_to_remote_tasks(string name, pending_frame* pfr){
// implementation
}
void listen(){
// implementation
}
void distributed_release(string task_name){
// implementation
}
};
Communicator communicator;
Does anyone know why this would be giving this error? The .cpp includes the header. I have other .cpp files that also include the header, but with the guard I don't see why that would matter.
Thanks for any help on this, much appreciated.
EDIT: Also, my runner.cpp file (with main) includes global.h in order to access the communicator global object.
You must have only one definition of a class. Currently you get one from #include and another in the file.
You shall not repeat the class itself, just implement the functions out of class, like
Communicator::Communicator(){
// implementation
}
That's not how you do the separation. The class (i.e. the declaration) goes into the header; the CPP file should have method implementations, like this:
Communicator::Communicator() {
...
}
void Communicator::stop_listening() {
...
}
and so on. Note the Communicator:: part of the fully qualified name: this is what tells the compiler that the function that you are defining belongs to the Communicator class.
In your cpp file you only need to define the functions that were declared but not defined in the header and scope them to your class:
Communicator::Communicator(){
// implementation
}
void Communicator::stop_listening(){
// implementation
}
void Communicator::add_to_remote_tasks(string name, pending_frame* pfr){
// implementation
}
void Communicator::listen(){
// implementation
}
void Communicator::distributed_release(string task_name){
// implementation
}
You define the class Communicator in the header file, and then try to add to it in the .cpp file. You can't do that in C++ - all the parts of the class definition must be in the same place.
Your header file should probably contain all the member definitions and function declarations, and your .cpp should proceed to define the member functions like:
void Communicator::stop_listening() { ... }
consider a fun.cpp file :
class fun
{
public:
void sum();
void dispaly();
};
Class fun2
{
public:
void subtract();
};
Now consider another c++ file execute.cpp where i want to access only subtract method of fun.cpp file..
i dont want to include "fun.cpp" file into my execute.cpp as it will increase the size(in larger projects)..
so, how can i access any particular method wihtod including the file????
i dont want to include "fun.cpp" file into my execute.cpp
Nor should you, as it would break the one definition rule (assuming the implementations are also in the cpp file).
The usual way to do this is to have the class definition in a header, and include only the header file.
To answer your question, you can, but it's fugly.
C++ allows us to define a class multiple times, as long as the definition is identical. So, in execute.cpp you can simply write:
//execute.cpp
class fun2 //note lower-case 'c'
{
public:
void subtract();
};
// use fun2 here
before you use it. But, again, the usual way is to break the class definition and implementation in a .h file and a .cpp file.
Mandatory advice: read a good introductory C++ book.
You need to include the header file which defines the class fun(and has the declaration of subtract()) in the cpp file where you want to use the function subtract().
Note that the function must be defined though.
fun2.h
#ifndef FUN2_H
#define FUN2_H
Class fun2
{
public:
void subtract();
};
#endif // FUN2_H
fun2.cpp
#include "fun2.h"
void func2::subtract()
{
}
execute.cpp
#include "fun2.h"
//use subtract() through object of `fun2`
Note that, to use a member function in a particular source file, the definition of the class which declares that function should be visible to the compiler, the actual job of linking to the particular definition is done at the linking stage and as long as you follow the above format the linker shall link the appropriate function for you.
Even though you include the file, the linker will only include code that is actually used. This is why using static libraries is sometimes preferable to a dynamic library that has to include everything.
You can't. You need to actually include the file that links to the code that defines the function if you want to include the function. If you just need the interface and aren't going to use the function, you could simply declare the class in execute.cpp as follows
class fun
{
public:
void subtract();
};
But you couldn't use subtract there.