0

**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
Georgez
  • 53
  • 9

2 Answers2

1

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

Community
  • 1
  • 1
Aadil Bhatti
  • 81
  • 1
  • 6
1

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.

Francis Cugler
  • 7,788
  • 2
  • 28
  • 59