C++ Error, Undefined Reference Class

C++ error 'Undefined reference to Class::Function()'

What are you using to compile this? If there's an undefined reference error, usually it's because the .o file (which gets created from the .cpp file) doesn't exist and your compiler/build system is not able to link it.

Also, in your card.cpp, the function should be Card::Card() instead of void Card. The Card:: is scoping; it means that your Card() function is a member of the Card class (which it obviously is, since it's the constructor for that class). Without this, void Card is just a free function. Similarly,

void Card(Card::Rank rank, Card::Suit suit)

should be

Card::Card(Card::Rank rank, Card::Suit suit)

Also, in deck.cpp, you are saying #include "Deck.h" even though you referred to it as deck.h. The includes are case sensitive.

C++ Error: Undefined Reference When Initializing a Class Object

As pointed out by Drew and Useless, I needed to compile my program first to resolve the error. I compiled the program and it ran smoothly.

I have a trouble when trying to initialize a function in struct C++

In the C++ programming language, you can define a struct just like you define a class. The reason you're getting the error is because you haven't defined the methods strictly.

#include <iostream>

using namespace std;

struct Date
{
/* fields */
int _year, _month, _day;

/* constructor */
Date(int year, int month, int day) : _year(year), _month(month), _day(day){}

/* setter method */
void add_day(int n)
{
/* You need to develop an algorithm to keep track of the end of the month and the year. */
_day += n;
}

/* getter method */
int get_day()
{
return _day;
}
};

int main()
{
Date today(2021, 1, 6);
today.add_day(1);
cout << today.get_day() << endl;
return 0;
}


Related Topics



Leave a reply



Submit