Including header files in C++ (class definition and method implementation) - c++

I have already checked StackOverflow to find the solution to my problem, but I think I might be missing something. I am trying to define a class in a header file (.h) and implement its methods in a cpp file (.cpp), but it does not work.
main.cpp:
#include <iostream>
#include "Message.h"
using namespace std;
int main()
{
Message *t = new (Message);
t->display();
return 0;
}
Message.h:
#ifndef MESSAGE_H_INCLUDED
#define MESSAGE_H_INCLUDED
class Message {
public:
void display();
};
#endif // MESSAGE_H_INCLUDED
Message.cpp:
#include "Message.h"
void Message::display() {
cout << "Hello!";
}
I don't understand why I keep getting the following error
undefined reference to 'Message::display()'

Compile this with the command g++ -std=c++11 Message.cpp main.cpp

Related

How to resolve a C++ Error: Redefinition of 'class'

I am new to C++ programming and have a compiler error that I can't figure out. Any help would be appreciated.
Here is the build log:
C:\Dev\MemberTest\Entity.cpp|6|error: redefinition of 'class Entity::Entity'|
C:\Dev\MemberTest\Entity.h|6|error: previous definition of 'class Entity::Entity'|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
The program has Main.cpp, Entity.h and Entity.cpp (I was just tinkering with how to implement headers and source files).
#include <iostream>
#include "Entity.h"
int main()
{
Entity::Entity Person("Grant", true); //Create person and set membership
std::cout << Person.getName() << " is a member: " << Person.getMembership() << std::endl;
return 0;
}
#ifndef ENTITY_H_INCLUDED
#define ENTITY_H_INCLUDED
namespace Entity
{
class Entity
{
private:
std::string name;
bool member;
public: //Get, set, constructor calls for a bool and string.
Entity(std::string y, bool x);
bool getMembership();
std::string getName();
void setMembership(bool x);
};
}
#endif // ENTITY_H_INCLUDED
#include <string>
#include "Entity.h"
namespace Entity
{
class Entity
{
private:
std::string name;
bool membership;
public:
Entity(std::string y, bool x):name(y),membership(x){}
bool getMembership(){return this->membership;};
std::string getName(){return this->name;};
void setMembership(bool x){this->membership=x;};
};
}
I've looked around for a solution and found questions like this: error: redefinition of class but the solutions I'm seeing aren't relevant to my program because I'm already using #ifndef.
Since I'm not sure what other info might be needed here goes: All three files are in the same folder and there aren't other source or header files in that folder. Oddly enough if I comment out the #include "Entity.h" in the Entity.cpp file and reference the source in Main.cpp instead of Entity.h it compiles and runs fine. I'm coding on Code::Blocks and with the GCC Compiler. Thanks again for any help.
The implementation file (Entity.cpp) should not contain the class definition again. Instead, you write non-inline definitions ("out of class"):
#include <string>
#include "Entity.h"
namespace Entity
{
Entity::Entity(std::string y, bool x) : name(y), membership(x) {}
bool Entity::getMembership() { return membership; }
std::string Entity::getName() { return name; }
void Entity::setMembership(bool x) { membership = x; }
}
Also note that your Entity.h header depends on std::string which requires the #include <string> header there, not just in the implementation file (Entity.cpp). There is no need to use this-> here nor some of the semicolons (;).
Oddly enough if I comment out the #include "Entity.h" in the Entity.cpp file and reference the source in Main.cpp instead of Entity.h it compiles and runs fine
That is because you can define functions inline in the class (instead of putting them in the implementation file). What you did is implement all of them in the class definition, and therefore you don't need an implementation file anymore.
In other words, your Entity.cpp looked like a header file with a full implementation of the class, although you called it .cpp rather than .h. Thus if you include that file, it would work.

C++ class in separate file not compiling. Already defined in Class.obj one or more multiply defined symbols found

So I've done extensive googling and searching on StackOverflow and am unable to find a solution despite several answers with this exact issue.
I am trying to create a test class in an external file called Fpc5.cpp
It's contents are:
Fpc5.cpp
#include "stdafx.h"
#include "Fpc5.h";
#include <iostream>
using std::cout;
class Fpc5 {
int bar;
public:
void testMethod();
};
void Fpc5::testMethod() {
cout << "Hey it worked! ";
}
and my main .cpp file:
Test.cpp
// Test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
//#include "Fpc5.cpp"
#include "Fpc5.h";
using std::cout;
using std::cin;
using std::endl;
int main()
{
cout << "Hello" << endl;
Fpc5 testObj;
testObj.testMethod();
system("pause");
return 0;
}
all the answers I've read indicate this is caused becaused I used to be including the class in the main file itself which is why I created a header file
Fpc5.h
#pragma once
void testMethod();
This changed the error, but still did not fix the issue. Currently my Test.cpp does not recognize a Fpc5 class. I've also tried adding the Fpc5.cpp and Fpc5.h in stdafx.h and that still does not resolve the issue.
stdafx.h
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
// TODO: reference additional headers your program requires here
//#include "Fpc5.cpp"
#include "Fpc5.h"
I'm sure this a simple syntax/conceptual understanding error, but I'm quite new to c++ and am not sure what is wrong.
This is definition of your class and it must be in Fpc5.h
class Fpc5 {
int bar;
public:
void testMethod();
};
Then, you have Fpc5.cpp where you implement methods of the class:
#include "Fpc5.h" // Compiler needs class definition to compile this file!
void Fpc5::testMethod()
{
}
And then you can use Fpc5 class in Test.cpp
#include "Fpc5.h"
int main()
{
Fpc5 foo;
foo.testMethod();
return 0;
}
As an alternative you can pack everything into Test.cpp
Move the definition of your class:
class Fpc5 {
int bar;
public:
void testMethod();
};
to the header file, "Fpc5.h".
Implement the methods to "Fpc5.cpp".

Error when using namespaces - class not declared

I'm new to C++ and using namespaces and I can't see what I'm doing wrong here. When I compile the code below, I get the error:
error: 'Menu' has not been declared
Here is my header file Menu.hpp
#ifndef MENU_H //"Header guard"
#define MENU_H
namespace View
{
class Menu
{
void startMenu();
};
}
#endif
and my Menu.cpp:
#include "stdio.h"
using namespace std;
namespace View
{
void Menu::startMenu()
{
cout << "This is a menu";
}
}
You missed including the header file which defines the class.
Menu.cpp:
#include "Menu.hpp"
Each translation unit is compiled separately by the compiler and if you do not include the header file in Menu.cpp, there is no way for the compiler to know what Menu is.
You will have to include header Menu.hpp in your cpp file Menu.cpp like
#include "Menu.hpp"

'Undefined reference to Class::method()'

Recently I've been learning how to create methods within classes so that I only have to write a method once and for each of that class I instantiate I can call the one method and it will work only on the variables of the object that called it, I know how to do this when only using main.cpp and no headers however I am confused on how I should be writing this when I use a class header and cpp.
I have a sample of code similar to what I want to achieve:
#include <iostream>
using namespace::std;
class Object
{
public:
int stuff;
void manageStuff();
Object();
};
void Object::manageStuff()
{
stuff++;
}
Object::Object() : stuff(0) {}
Object object1, object2;
int main() {
for (int i = 0; i < 10; i++)
{
object1.manageStuff();
object2.manageStuff();
cout << object1.stuff << "\n";
cout << object2.stuff << "\n";
}
}
This works fine and allows me to have two instances of Object and a method that works independently for each instance, this is my current project:
main.cpp:
#include <iostream>
#include "Test.h"
using namespace std;
int main()
{
Test test;
for (int i = 0; i < 10; i++)
{
test.count(); // Here's my error "undefined reference to Test::count"
}
return 0;
}
Test.cpp
#include <iostream>
#include "Test.h"
using namespace std;
Test::Test()
{
//ctor
}
Test::~Test()
{
//dtor
}
Test.h
#include <iostream>
#ifndef TEST_H
#define TEST_H
class Test
{
public:
Test();
virtual ~Test();
void count();
int counter();
};
#endif // TEST_H
and finally TestFunctions.h
#include <iostream>
#include "Test.h"
#ifndef TESTFUNCTIONS_H_INCLUDED
#define TESTFUNCTIONS_H_INCLUDED
void Test::count()
{
Test::counter++;
std::cout << Test::counter;
}
#endif // TESTFUNCTIONS_H_INCLUDED
I'm sure that there will be something that's very obviously wrong to a more seasoned programmer and I'm about to look a bit thick but any help would be greatly appreciated
Thanks!
I would suggest getting rid of TestFunctions.h, and adding the implementation of Test::count() to Test.cpp. Currently, the TestFunctions.h header is not included anywhere, so you have no access to the definition from main.
You defined (i.e. implemented) Test::count() in a header file (TestFunctions.h), but you never included it anywhere so the code there is not compiled.
You should change it to be in a .cpp file, compile it and link it with the other source files. There's no reason why not to place it in Test.cpp.
Rename TestFunctions.h into TestFunctions.cpp, make it compiled same way as main.cpp and linked.
Alternatively, include TestFunctions.h somewhere, e.g. main.cpp

Error : **** has not been declared

In my Function.h file:
class Function{
public:
Function();
int help();
};
In my Function.cpp file:
#include "Function.h"
int Function::help() //Error here
{
using namespace std;
cout << "Help";
return 1;
}
In my Main.cpp
#include <iostream>
#include "Function.h"
using namespace std;
int menu(){
Function fc;
fc.help();
return 1;
}
int main(int args, char**argv){
return menu();
}
Error is : ‘Function’ has not been declared
Can anybody tell me why? Thank you.
I tried like this and the problem is solved, but I dont really understand why:
In Function.h file:
I use
class Function{
public:
int status;
Function():status(1){}
int help();
};
instead of the old one
class Function{
public:
Function();
int help();
};
All your include statements are missing the #:
#include "Function.h"
^
Everything else looks fine, though you need to also #include <iostream> in Function.cpp since you're using cout.
Here is the Function.cpp that I got to compile and run:
#include "Function.h"
#include <iostream>
int Function::help() // No error here
{
using namespace std;
cout << "Help";
return 1;
}
Function::Function()
{
}
I had a similar problem. Make sure that you only have the required header files. I had two header files both including each other and it spit out this mistake.
You have created a declaration for the constructor of the Function class without including it in your implementation (cpp file).
#include "Function.h"
Function::Function(){
// construction stuff here
}
int Function::help() //Error here
{
using namespace std;
cout << "Help";
return 1;
}
In the first Function.h file you have declared the constructor but not defined it. In the second Function.h file (the one that works) you have defined and declared the Function constructor. You can either define and declare in the header or file, or declare in the header file and define in the Function.cpp file.
For example, declare in the header file "Function.h":
class Function
{
Function();
}
and define here in "Function.cpp":
Function::Function(){}
Or the alternative is to declare and define in the header file "Function.h":
Class Function
{
Function(){}
}
The other thing that you have done in the second version of the header file is to initialise the member variable "status" in the "member initialisation list" which is a good thing to do (See Effective C++ by Scott Meyers, Item 4). Hope this helps :)