'getting "error: redefinition of class" in cpp file even though I am not trying to redefine a class [closed]
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
Solution 1:[1]
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.
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Cory Kramer |
| Solution 2 |
