undefined reference to `Game::play()' [duplicate] - c++

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 1 year ago.
I get the following error message when I try to invoke Game::play() member function, I've been searching for what could be the error. But I couldn't find it.
Here's Game.h
#pragma once
#include <iostream>
class Game {
public:
void selectPlayer();
// Player* nextPlayer() const;
bool isRunning() const;
void play();
void announceWinner();
};
Game.cpp implementation
#include "Game.h"
void Game::selectPlayer() {
}
bool Game::isRunning() const {
}
void Game::play() {
// while (isRunning()) {
// board.display();
// }
//board.display();
std::cout << "hello\n";
}
void Game::announceWinner() {
std::cout << "Game is over\n";
}
main.cpp
#include <iostream>
#include "Game.h"
int main() {
Game g;
g.play();
}
I get this error when I invoke play(): undefined reference to Game::play()'`

At a glance your code looks okay. I would suggest adding a class constructor to your .h file:
#pragma once
#include <iostream>
class Game {
public:
Game(void);
...
Also ensure you have return 0; at the end of your main still.

Related

error while using std:sort on vector of objects [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 3 years ago.
I have a vector of Person object pointers. I am trying to use std:sort to sort the vector based on the "name" of each object. I am getting an unresolved external symbol error when I try to build and run; can anyone see where I am going wrong? error:
Error LNK2019 unresolved external symbol "public: static bool __cdecl Person::sortByName(class Person *,class Person *)" (?sortByName#Person##SA_NPAV1#0#Z) referenced in function _main Lab1b C:\Users\jayjo\source\repos\Lab1b\Lab1b\Lab1b.obj 1
error
Main cpp:
#include <iostream>
#include "Person.h"
#include "Employee.h"
#include "Customer.h"
#include <vector>
#include <algorithm>
int main()
{
vector<Person*> people;
people.push_back(new Person("Peter"));
people.push_back(new Person("John"));
people.push_back(new Person("David"));
people.push_back(new Person("Aaron"));
sort(people.begin(), people.end(), Person::sortByName);
for (int i = 0; i < people.size(); i++)
{
cout << people[i]->getName() << endl;
}
}
Person.h:
#pragma once
#ifndef Person_H
#define Person_H
using namespace std;
#include <iostream>
class Person
{
public:
Person(string);
virtual void printname();
static bool sortByName(Person* A, Person* B);
string getName();
protected:
string name;
};
#endif // !Person_H
Person.cpp:
#include "Person.h"
using namespace std;
Person::Person(string n)
{
name = n;
}
void Person::printname()
{
cout << "Name: " << name << endl;
}
string Person::getName()
{
return name;
}
static bool sortByName(Person* A, Person* B)
{
return (A->getName().compare(B->getName()));
}
Instead of this:
static bool sortByName(Person* A, Person* B)
{
return (A->getName().compare(B->getName()));
}
This:
bool Person::sortByName(Person* A, Person* B)
{
return (A->getName().compare(B->getName()) != 0);
}
In C++, you declare the class member function as static, but you leave the static keyword off when you define it. Also, the function needs to be defined as a class member (Person::).

C++ compiling error

I have a compiling error in C++ using classes. I have worked with classes before and have never encountered this error. I have tried adding static before the method ImprtData but that only prompted more errors.
error: invalid use of non-static member function bank.ImprtData;
here is my .cpp
#include "componets.h"
User::User() {
std::cout << "loaded" << std::endl;
}
void User::ImprtData() {
std::cout << "loaded.\n";
}
and here is my .h
#include <sstream>
#include <fstream>
#include <vector>
#include <iostream>
#include <string>
class User {
public:
User();
void write();
void launch_main_menu();
void login();
void ImprtData();
private:
void deposit();
void withdrawl();
std::string account_name;
int account_pin;
float account_balance;
std::string account_user_name;
};
and finally my main
#include "componets.h"
int main() {
std::cout << "Welcome to Bank 111.\n";
User bank;
bank.ImprtData;
return 0;
}
This is essentially a simple typo. Replace
bank.ImprtData;
with
bank.ImprtData();
to call the function. The expression bank.ImprtData is confusing the compiler since it's interpreting it as the address of a function, and issues a diagnostic since the function is not static.
bank.ImprtData; should be bank.ImprtData();

How to import a class in another C++ header file? [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 6 years ago.
I meet a problem when constructing a class. class "Graph" import a class "Bag" in another file and use "Bag" as its component.
//Graph.h
#ifndef GRAPH_H
#define GRAPH_H
#include <fstream>
#include <iostream>
#include <vector>
#include "Bag.h"
class Bag;
class Graph
{
public:
Graph(int V);
Graph(std::ifstream& in_file);
int getV() { return V; }
int getE() { return E; }
void addEdge(int v, int w);
void showadj() ;
private:
int V;
int E;
std::vector<Bag> adj;
};
#endif
And "Bag.h" is as follow:
//Bag.h
#ifndef BAG_H
#define BAG_H
#include <vector>
#include <iostream>
class Bag
{
public:
Bag();
void addBag(int i) { content.push_back(i); }
void showBag();
private:
std::vector<int> content;
};
#endif
Graph.cpp:
//Graph.cpp
#include "Graph.h"
#include "Bag.h"
Graph::Graph(int V) : V(V), E(0)
{
for (int i = 0; i < V; i++)
{
Bag bag;
adj.push_back(bag);
}
}
Bag.cpp(sorry, forget it):
#include "Bag.h"
void Bag::showBag()
{
for (int i : content)
{
std::cout << i << " ";
}
}
When I try to complie these two classes, an error comes out saying:
C:\Users\ADMINI~1\AppData\Local\Temp\ccMj4Ybn.o:newtest.cpp:(.text+0x1a2): undef
ined reference to `Bag::Bag()'
collect2.exe: error: ld returned 1 exit status
You need to also implement your constructor Bag::Bag() as it is missing in your Bag.cpp file.
This is what the error tells you. If you don't need the constructor, then you should remove it from your class definition, which would also solve this error.
An alternative would be to provide an empty constructor in your Bag.h file
class Bag
{
public:
Bag() {}
...
}

Undefined reference to class::method [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
In these days I thought I would try to work on some C++ Object Oriented Programming by converting one of my old programs to an object which methods I could use in other programs.
The program is able to get an array of strings and print them to make a selection screen like this:
"> Stuff"
" Stuff2"
" Stuff3"
And the cursor could then be moved with the keyboard arrows and one entry could be selected by pressing Enter.
Unfortunately, after having removed most errors the only left is a "Undefined reference to class::method()" for each method that I wrote.
The code is the following:
Selection.cpp
#include "selection.h"
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <cstring>
#include <string>
#include <vector>
std::vector<entry> selectedArray;
int posizione = 1, tastoPremuto;
void setCurrentArray(std::string selectionEntries[])
{
//Copies a string array to a vector of struct entry
}
void resetSelection()
{
//Blanks out every entry.segno
}
void selectionUpdate(int stato)
{
//Moves the cursor
}
int getPosition()
{
return posizione; //Returns the position of the cursor
}
void setPosition(int nuovaPosizione) //Sposta il puntatore alla nuovaPosizione
{
posizione = nuovaPosizione; //Sets the posiion of the cursor
}
entry getCurrentEntry(int posizioneCorrente) //Returna l'entry della posizione attuale
{
//Gets the name of the entry at which the cursor is
}
void printEntries() //Stampa tutte le entries con la selezione
{
//Prints all the entries with the cursor before them
}
int getSelection(bool needConfirm)
{
//gets the selection from keyboard
}
Selection.h
#ifndef SELECTION_H
#define SELECTION_H
#include <string>
#include <vector>
struct entry
{
std::string entryName;
char sign;
};
class selection
{
public:
selection();
virtual ~selection();
void setCurrentArray(std::string selectionEntries[]);
void resetSelection();
int getPosition();
void setPosition(int nuovaPosizione);
entry getCurrentEntry(int posizioneCorrente);
void printEntries(int argumentsNumber);
int getSelection(bool needConfirm);
protected:
private:
int tastoPremuto;
int posizione;
void selectionUpdate(bool stato);
std::vector<entry> selectedArray;
};
#endif // SELECTION_H
main.cpp
#include <stdio.h>
#include <iostream>
#include <conio.h>
#include <selection.h>
#include <vector>
#include <array>
std::string selezioneTestArray[3] = {"Prima selezione", "Seconda selezione", "Terza seleazione"};
int main()
{
selection sel;
sel.setCurrentArray(selezioneTestArray);
sel.setPosition(0);
sel.resetSelection();
sel.printEntries(3);
sel.getSelection(true);
return 0;
}
If you need the whole code, it's in my Github: https://github.com/LiceoFederici2H/Vita-Da-Lavoratore
Sorry if it's in Italian but I meant to use this also as a school project.
Thanks in advance.
In source file (Selection.cpp), you have to qualify the method names with class name:
void Selection::setCurrentArray(std::string selectionEntries[])
{
//Copies a string array to a vector of struct entry
}
Otherwise these methods will be free function, and not definition of the class methods. As a result, your class methods remain undefined, and when linker tries to link with the methods, it can not find them, and shows undefined reference of the method error.

"undefined reference to" while i am trying to inherit vector class [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 9 years ago.
Main.cpp
#include <iostream>
#include "include/Numbers.h"
#include <vector>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ofstream myofile;
ifstream myifile;
myofile.open("output.txt");
myifile.open("input.txt");
int number;
Numbers input;
if(myifile.is_open())
while(myifile >> number) {
input.push_back(number);
}
cout << input.size() << endl;
myofile.close();
myifile.close();
cout << "Hello world!" << endl;
return 0;
}
Numbers.h
#ifndef NUMBERS_H
#define NUMBERS_H
#include <vector>
class Numbers: public std::vector<int>
{
public:
Numbers();
~Numbers();
int size();
Numbers prob();
protected:
private:
};
#endif // NUMBERS_H
Numbers.cpp
#include "../include/Numbers.h"
#include <iostream>
using namespace std;
Numbers::Numbers()
{
}
Numbers::~Numbers()
{
//dtor
}
I am trying to create a new Numbers class which inherits functions from vector class.
The error I am getting is undefined reference to 'Numbers::size()' although the push_back function didn't give any problem
I am using codeblocks to write my code, and I have included all files in the build properties
First, what you are doing there is not a good idea. It is generally not intended to use STL-classes as base-classes (except for those especially designed for this, such as std::unary_function). std::vector does not have any virtual methods, so it does not have any value as a public base-class. Making things worse, std::vector does not even have a virtual destructor, so when you use it polymorphically, you will probably create a memory leak:
void deleteVector(std::vector<int>* v)
{
delete v;
}
deleteVector( new Numbers() );
In your case, you declared a Method Numbers::size which is not defined in the source-file. You could use a using declaration to use the size-method of your base-class, but that is not needed for public methods.
class Numbers: private std::vector<int>
{
public:
using std::vector<int>::size;
};