-2

I was coding something as a little practice and I kept getting this error:

error: redefinition of a "LabMouse::LabMouse(std::__cxx::string,int)

It's saying that it was already defined in my .h file, which it is. I'm just confused on why it's saying that I'm trying to redefine it within my .cpp file?

The .cpp file

#include <iostream>
#include "LabMouse.h" 

LabMouse::LabMouse(const std::string aname, const int aage){

}

void LabMouse::speak(){
        std::cout << "Narf!" << std::endl;
}

LabMouse::~LabMouse(){
        std::cout << "LabMouse Destructor" << std::endl;
}

void LabMouse::setName(std::string name){
        name = "Pinky";
}

void LabMouse::sayHello(){
        std::cout << "LabMouse Name: " << aname << " ,age: " << aage << std::endl;
}

The .h file

#ifndef LABMOUSE_H
#define LABMOUSE_H
#include <string>

class LabMouse{
private:
        std::string aname = "";
        int aage = 1;
public:
        LabMouse(const std::string aname, const int aage){

        }
        void speak();
        void setName(std::string aname);
        void sayHello();


        virtual ~LabMouse();
};

#endif
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 2
    The error is correct. You define the `LabMouse` constructor in the header, and again in the cpp file. This violates the [One Definition Rule](https://stackoverflow.com/questions/4192170/what-exactly-is-one-definition-rule-in-c). The **complete** error message should also be telling you [the exact line where this is happening](https://godbolt.org/z/5brKKr3dv). – Drew Dormann Feb 09 '22 at 19:52
  • You did provide a body for `LabMouse::LabMoude(std::string, int)` in the class definition. If this isn't on purpose, you need to replace the constructor body with a semicolon in the header... – fabian Feb 09 '22 at 19:52
  • 1
    Note that you are not _trying to redefine a class_, you are actually trying to redefine a **constructor** – Gian Paolo Feb 09 '22 at 19:56
  • You have to read error message very carefullly; that takes practice. The compiler is complaining about `LabMouse::LabMouse`, which is the constructor for the class `LabMouse`. – Pete Becker Feb 09 '22 at 20:22

2 Answers2

2

In the header file remove the braces (thereby turning your definition into a declaration), otherwise you are providing an implementation

LabMouse(const std::string aname, const int aage){
}

should just be

LabMouse(const std::string aname, const int aage);

because you have an implementation already in the cpp file.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
0

You are defining a body for LabMouse() in the .h file, and you are defining another body for LabMouse() in the .cpp file. Hence the error. You need to get rid of one of those bodies.

Also, your constructor is not initializing the aname and aage class members with the values of the input parameters.

Use this instead:

#include <iostream>
#include "LabMouse.h" 

LabMouse::LabMouse(const std::string aname, const int aage)
    : name(aname), age(aage) {
}

void LabMouse::speak() const {
    std::cout << "Narf!" << std::endl;
}

LabMouse::~LabMouse() {
    std::cout << "LabMouse Destructor" << std::endl;
}

void LabMouse::setName(const std::string &aname) {
    name = aname;
}

void LabMouse::sayHello() const {
    std::cout << "LabMouse Name: " << name << ", age: " << age << std::endl;
}
#ifndef LABMOUSE_H
#define LABMOUSE_H
#include <string>

class LabMouse{
private:
    std::string name;
    int age = 1;
public:
    LabMouse(const std::string aname, const int aage);
    virtual ~LabMouse();

    void speak() const;
    void setName(const std::string &aname);
    void sayHello() const;
};

#endif

Or this:

#include <iostream>
#include "LabMouse.h" 

void LabMouse::speak() const {
    std::cout << "Narf!" << std::endl;
}

LabMouse::~LabMouse() {
    std::cout << "LabMouse Destructor" << std::endl;
}

void LabMouse::setName(const std::string &aname) {
    name = aname;
}

void LabMouse::sayHello() const {
    std::cout << "LabMouse Name: " << name << ", age: " << age << std::endl;
}
#ifndef LABMOUSE_H
#define LABMOUSE_H
#include <string>

class LabMouse{
private:
    std::string name;
    int age = 1;
public:
    LabMouse(const std::string aname, const int aage)
        : name(aname), age(aage)
    {
    }

    virtual ~LabMouse();

    void speak() const;
    void setName(const std::string &aname);
    void sayHello() const;
};

#endif
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770