Ttrying to understand Classes and headers - c++

**PROBLEM SOLVED. It appears that i had created an extra header by mistake, and since i deleted him , it worked. **
So i am trying to understand about classes and headers and how i can make them work together.
I am following an online tutorial but it seems that something is going wrong in my code.
The problem is that when i try to run the main it gives me this error:
multiple definition of Cat::speak() and all the other functions.
main.cpp
#include <iostream>
#include "class.h"
using namespace std;
int main()
{
Cat jim;
jim.makehappy();
jim.speak();
Cat george;
george.makesad();
george.speak();
return 0;
}
class.cpp
#include <iostream>
#include "class.h"
using namespace std;
void Cat::speak()
{
if (happy)
{
cout << "meoww" << endl;
}
else
{
cout << "sssss" << endl;
}
}
void Cat::makehappy()
{
happy = true;
}
void Cat::makesad()
{
happy = false;
}
class.h
#ifndef CLASS_H_INCLUDED
#define CLASS_H_INCLUDED
class Cat
{
private:
bool happy;
public:
void makehappy();
void makesad();
void speak();
};
#endif // CLASS_H_INCLUDED

From what you have shown here there should be no problems. What you could do to temporarily resolve this to find out if you are actually defining this function in several places is to wrap your class in a namespace.
class.h
#ifndef CLASS_H_INCLUDED
#define CLASS_H_INCLUDED
namespace myNamespace {
class Cat {
private:
bool happy;
public:
void makehappy();
void makesad();
void speak();
};
} // namespace myNamespace
#endif // CLASS_H_INCLUDED
class.pp
#include <iostream>
#include "class.h"
// using namespace std; // Don't Use - This is Bad Practice
// Can cause name clashing when trying to resolve name lookup
namespace myNamespace {
void Cat::speak() {
if (happy) {
std::cout << "meoww" << std::endl;
} else {
std::cout << "sssss" << std::endl;
}
}
void Cat::makehappy() {
happy = true;
}
void Cat::makesad() {
happy = false;
}
} // namespace myNamespace
main.cpp
#include <iostream>
#include "class.h"
// using namespace std; // Again -Bad Practice
int main() {
using namespace myNamespace;
Cat jim;
jim.makehappy();
jim.speak();
Cat george;
george.makesad();
george.speak();
return 0;
}
Try this to see if you are getting the same compiler error. This should help you to see if you are defining this function in multiple spaces. Also by removing the using namespace std; and just using the scope resolution operator to the std:: namespace will help to eliminate any possible problems and any possible future problems.

How are you compiling the code? You need to make sure that you are building the specific "class.o" and "main.o" files separately before linking them together. Here is an example Makefile.
all: main
main: main.o class.o
g++ main.o class.o -o main
main.o: main.cpp class.h
g++ -c main.cpp
class.o: class.cpp class.h
g++ -c class.cpp
It looks like you are using double inclusion guards so I don't think that is the problem. Check out this answer for a more in-depth explanation of what is happening: Error with multiple definitions of function

Related

'cout' is not a member of 'std' (Progect File c++)

I created a new project in c++ but i keep getting the same error
Main.cpp
#include <iostream>
#include <string.h>
#include "Computer.cpp"
#include "Computer.h"
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
Computer.h
#ifndef COMPUTER_H_INCLUDED
#define COMPUTER_H_INCLUDED
#include <string>
class Computer
{
public:
std::string marca;
float prezzo;
bool acceso;
Computer();
void Accenditi();
void Spegniti();
void ImpostaMarca(std::string m);
void ImpostaPrezzo(float p);
};
#endif
Computer.cpp
#include "Computer.h"
Computer::Computer()
{
}
void Computer::Accenditi()
{
if(!acceso)
{
acceso = true;
}
else
{
std::cout << "Sono già acceso";
}
}
void Computer::Spegniti()
{
if(acceso)
{
acceso = false;
}
else
{
std::cout << "Sono già spento";
}
}
void Computer::ImpostaMarca(std::string m)
{
marca = m;
}
void Computer::ImpostaPrezzo(float p)
{
prezzo = p;
}
The Problem
i don't understand what's wrong with Computer.cpp, i keep getting "cout is not a member of std". I tryed to add "using namespace std" and i also tryed to add the library #include but i get a new file called "makefile.win". How can i fix this error ?
You need to include iostream header in your Computer.cpp file as such:
include <iostream>
and to make your life easier, you can also add:
using std::cout;
using std::endl;
right at the bottom of your include, that way you don't have to keep adding "std::cout" everytime, you can just use "cout"
Also want to add:
You can remove the include computer.cpp from your main.cpp and just leave the header. The C++ linker will automatically link your computer.h and computer.cpp together since .cpp includes the header, and your main includes the computer.h
Add # include <iostream> at the files that you use std::cout and std::cin.

c++ 'undefined reference to' error

I'm having one of those "undefined reference to " errors when compiling a c++ program. I know this is common pitfall, but so far was unable to figure out what I'm doing wrong.
Here's the relevant code. Ex1Two_Sum.h:
#ifndef EX1TWO_SUM_H
#define EX1TWO_SUM_H
#include <vector>
using namespace std;
namespace ddc {
class Ex1Two_Sum
{
public:
void f();
protected:
private:
};
}
#endif
Ex1Two_Sum.cpp:
#include <vector>
#include <cstddef>
#include <iostream>
using namespace std;
namespace ddc {
class Ex1Two_Sum {
public:
void f(){
cout << "works" << endl;
}
};
}
And finally, main.cpp:
#include <iostream>
#include "Ex1Two_Sum.h"
using namespace std;
using namespace ddc;
int main()
{
Ex1Two_Sum ex1;
ex1.f();
return 0;
}
I compile as follows:
g++ -std=c++11 -c Ex1Two_Sum.cpp
g++ -std=c++11 -c main.cpp
g++ Ex1Two_Sum.o main.o
yielding the following message:
main.o: In function `main':
main.cpp:(.text+0x2c): undefined reference to `ddc::Ex1Two_Sum::f()'
collect2: error: ld returned 1 exit status
Your source file redefines the whole class, with an inline function definition, when it just needs to provide a non-inline function definition.
#include "Ex1Two_Sum.h"
void ddc::Ex1Two_Sum::f() {
std::cout << "should work\n";
}
Also, please don't put using namespace std; in a header. Not everyone wants the global namespace polluted in potentially surprising ways.
First, which line of the command throws that error?
Second, I think you forgot to include the Ex1Two_Sum.h in the Ex1Two_Sum.cpp
Third you need to change class ....... in Ex1Two_Sum.cpp to:
void Ex1Two_Sum::f(){...}

Cannot create a method in a class

I am new to c++ but have had some experience with java. I am trying to create a class but when i attempt to make a new method in the class I get several error (this is my .cpp file)
//.cpp file
#include "Test.h"
#include "Test.h"
#include <iostream>//unresolved inclusion
using namespace std;
void speak() {
if (happy) {//Symbol hapy could not be resolved
cout << "Meouw!" << endl;
} else {
cout << "Ssssss!" << endl;
}
}
void Test::makeHappy() { // member decleration not found
happy = true;//Symbol hapy could not be resolved
}
void Test::makeSad() { // member decleration not found
happy = false;//Symbol hapy could not be resolved
}
I dont get any errors in my heder file but have included it just in case
#ifndef TEST_H_
#define TEST_H_
class Test {
private:
bool happy;
public:
void makeHappy();
void makeSad();
void speak();
};
#endif /* TEST_H_ */
Finally I have another .cpp file I use which also gets errors
#include <iostream>//unresolved inclusion
#include "Test.h"
#include "Test.cpp"
using namespace std;
int main() {
Test jim;
jim.makeHappy();//method make happy could not be resolved
jim.speak();//method speak could not be resolved
Test bob;
bob.makeSad();//method make happy could not be resolved
bob.speak();//method speak could not be resolved
return 0;
}
This is the new error message I get when compiling
<!-- begin snippet: js hide: false -->
Sorry if this question is to open ended but I just cannot seem to find answers elsewhere.
this is your header file Test.h:
happy member was named bhappy_
for a private member think to add a getter and setter public member functions
#ifndef TEST_H_
#define TEST_H_
class Test {
private:
bool bhappy_;
public:
Test() // ctor
virtual ~Test() // dtor
public:
void makeHappy();
void makeSad();
void speak();
};
#endif /* TEST_H_ */
this is your Test.cpp file:
It's not advised to use using namespace std;
Don't include your header two times
//.cpp file
#include "Test.h"
#include <iostream>//unresolved inclusion
//ctor
Test::Test() : bhappy_(false)
{}
Test::~Test(){}
void Test::speak() {
if (bhappy_) {//Symbol hapy could not be resolved
std::cout << "Meouw!" << endl;
} else {
std::cout << "Ssssss!" << endl;
}
}
void Test::makeHappy() {
bhappy_ = true;//Symbol hapy could not be resolved
}
void Test::makeSad() { // member decleration not found
bhappy_ = false;//Symbol hapy could not be resolved
}
This is your main function:
#include "Test.h"
int main(int argc, char** argv) {
Test jim;
jim.makeHappy();//method make happy could not be resolved
jim.speak();//method speak could not be resolved
Test bob;
bob.makeSad();//method make happy could not be resolved
bob.speak();//method speak could not be resolved
return 0;
}
You need specify Test::speak().
Take should use this. to use class attribut. Think about getter and setter.
And finally, look C++ training, it should be usefull ^^ .
See you.

Multiple definition of function in C++

I am having a very unusual problem:
I keep getting multiple definition of functions in my class.
This is my main .cpp
#include <iostream>
#include "Calculation.cpp"
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
this is my class .h
#ifndef CALCULATION_H_INCLUDED
#define CALCULATION_H_INCLUDED
class Calculation
{
public:
Calculation();
private:
};
#endif // CALCULATION_H_INCLUDED
this is my implementation file .cpp
#include "Calculation.h"
Calculation::Calculation()
{
}
Please help me; I have tried to create a new project but that didn't help.
All help is appreciated.
make your main.cpp like :
#include <iostream>
#include "Calculation.h" // not Calculation.cpp
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
You have to include your Calculation.h in th main.cpp and you have to compile it as follows,
g++ main.cpp Calculate.cpp -o main -I<path for your .h file>
main.cpp
#include<iostream>
#include "Calculation.h"
//using namespace std; // Avoid this, always to use std::cout .. etc on place
int main()
{
Calculation c; //Creating the object of Calculation class
std::cout<<"Hello World!"<<std::endl;
return 0;
}

'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