In: Computer Science
Hi, I don’t know why I keep getting this error and I’ve spent a while on it now. I am coding in C++.
Here is my .h
___
#ifndef CARD_H #define CARD_H #include <iostream> #include <string> using namespace std; class Card { private: int powerLevel; string element; public: Card(); Card(string, int); string getElement(); int getPowerLevel(); void displayCard(); }; #endif
___
Here is my .cpp
___
#include <iostream> #include "Card.h" #include <string> using namespace std; Card::Card() { element = ""; powerLevel = 0; } Card::Card(string e, int a) { element = e; powerLevel = a; } string Card::getElement() { return element; } int Card::getPowerLevel() { return powerLevel; } void Card::displayCard() { cout << element << "-" << powerLevel << endl; }
___
This is the error message
___
/tmp/ccOrFltm.o: In function `Card::Card()': Card.cpp:(.text+0x0): multiple definition of `Card::Card()' /tmp/ccRpEdKb.o:Card.cpp:(.text+0x0): first defined here /tmp/ccOrFltm.o: In function `Card::Card()': Card.cpp:(.text+0x0): multiple definition of `Card::Card()' /tmp/ccRpEdKb.o:Card.cpp:(.text+0x0): first defined here /tmp/ccOrFltm.o: In function `Card::Card(std::string, int)': Card.cpp:(.text+0x64): multiple definition of `Card::Card(std::string, int)' /tmp/ccRpEdKb.o:Card.cpp:(.text+0x64): first defined here /tmp/ccOrFltm.o: In function `Card::Card(std::string, int)': Card.cpp:(.text+0x64): multiple definition of `Card::Card(std::string, int)' /tmp/ccRpEdKb.o:Card.cpp:(.text+0x64): first defined here /tmp/ccOrFltm.o: In function `Card::getElement()': Card.cpp:(.text+0xd0): multiple definition of `Card::getElement()' /tmp/ccRpEdKb.o:Card.cpp:(.text+0xd0): first defined here /tmp/ccOrFltm.o: In function `Card::getPowerLevel()': Card.cpp:(.text+0xfe): multiple definition of `Card::getPowerLevel()' /tmp/ccRpEdKb.o:Card.cpp:(.text+0xfe): first defined here /tmp/ccOrFltm.o: In function `Card::displayCard()': Card.cpp:(.text+0x10e): multiple definition of `Card::displayCard()' /tmp/ccRpEdKb.o:Card.cpp:(.text+0x10e): first defined here collect2: error: ld returned 1 exit status
___
Thank you for your time
main.cpp
#include <iostream>
#include "Card.h"
#include <string>
using namespace std;
Card::Card()
{
element = "";
powerLevel = 0;
}
Card::Card(string e, int a)
{
element = e;
powerLevel = a;
}
string Card::getElement()
{
return element;
}
int Card::getPowerLevel()
{
return powerLevel;
}
void Card::displayCard()
{
cout << element << "-" << powerLevel << endl;
}
int main(){
Card c("AC",10);
c.displayCard();
}
Card.h
#ifndef CARD_H
#define CARD_H
#include <iostream>
#include <string>
using namespace std;
class Card
{
private:
int powerLevel;
string element;
public:
Card();
Card(string, int);
string getElement();
int getPowerLevel();
void displayCard();
};
#endif
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me