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.
Related
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.
I have 2 classes, Qtree and Qnode:
//QuadTree.hpp:
#include <stdio.h>
#include <iostream>
#include <SFML/Graphics.hpp>
class QNode;
class QTree{
public:
int depth;
sf::Vector2f bound;
QTree();
void setRoot(QNode q);
};
class QNode{
public:
bool isLeaf = false;
int layer = 0;
sf::Vector2f position;
sf::Vector2f bound;
};
and
//Quadtree.cpp:
#include "QuadTree.hpp"
QTree::QTree(){
bound = sf::Vector2f(100,200);
}
void QTree::setRoot(QNode q){
q.bound = bound;
}
then in main.cpp:
#include <iostream>
#include <SFML/Graphics.hpp>
#include "QuadTree.hpp"
using namespace std;
int main(){
QTree qt;
QNode q;
qt.setRoot(q);
cout << q.bound.x <<endl;
}
and the cout result is 0. I am expecting it to be 100.
I am c++ beginner and did read a lot related to this from google. However still not sure how to get this to work.
How can I resolve this?
You need to change setRoot() so it gets the argument by reference.
void QTree::setRoot(QNode &q){
q.bound = bound;
}
Your code is passing by value, so setRoot() modifies a copy of q.
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I am currently trying to create a simple implementation of a Module class and a Student class in c++. These classes will incorporate specific modules and the individual students enrolled in them. However, I can't seem to get round this particular error message whenever I try to make a Module or Student object.
It will probably be something simple I missed but I have uploaded my header and source files below. Please help, this is driving me crazy. Thanks in advance.
student.h:
#include "stdafx.h"
#include <string>
class Student {
public:
Student(std::string, std::string, int);
std::string getName() const { return name; }
std::string getDegree() const { return degree; }
int getLevel() const { return level; }
private:
std::string name;
std::string degree;
int level;
};
module.cpp:
// student.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "module.h"
#include <vector>
#include <iostream>
#include <string>
using namespace std;
void Module::enrol_student(Student studentY) {
students.push_back(studentY);
}
void Module::attendance_register() {
string nameX;
cout << "Attendance Register:\n" << endl;
for (int i = 0; i < students.size(); i++) {
Student studentX = students.at(i);
nameX = studentX.getName();
cout << nameX << endl;
}
}
module.h:
#include "stdafx.h"
#include "student.h"
#include <string>
#include <vector>
class Module {
public:
Module(std::string, std::string, std::vector<Student>);
std::string getModCode() { return modCode; }
std::string getModTitle() { return modTitle; }
void attendance_register();
void enrol_student(Student);
private:
std::string modCode;
std::string modTitle;
std::vector<Student> students;
};
testCode.cpp
// testCode.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
#include "module.h"
#include <vector>
#include <iostream>
#include <string>
using namespace std;
int main() {
//Initial Test Data
Student student1("Arthur Smith", "Computer Science", 1);
return 0;
}
You need to define the constructors you declared in your classes. In student.cpp you need something like this:
Student::Student(std::string name, std::string degree, int level) : name(name), degree(degree), level(level)
{
}
This will initialise the members with the values provided.
And similar for Module.
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;
};
So here is the main where i'm trying to call the array by a pointer:
#include <iostream>
#include "Lottery.h"
#include "Player.h"
#include "LotteryData.h"
using namespace std;
int main()
{
Player player;
Lottery random;
LotteryData data;
player.Input();
random.setRandomNumber();
data.PassInfo(int (&Numbers)[6][6]);
}
Apparently "Numbers" is undefined even though the header is included, here's the header and .cpp files relating to it.
LotteryData.h
#pragma once
#include <iostream>
#include <fstream>
#include "Lottery.h"
#include "Player.h"
using namespace std;
class LotteryData
{
private:
public:
LotteryData();
~LotteryData();
void PassInfo(int (&Numbers)[6][6]);
};
LotteryData.cpp
#include <iostream>
#include <fstream>
#include "LotteryData.h"
using namespace std;
LotteryData::LotteryData()
{
}
LotteryData::~LotteryData()
{
}
void LotteryData::PassInfo(int (&Numbers)[6][6])
{
int* ptr;
FILE *Numfile;
Numfile = fopen("C:/Num.txt", "wb");
ptr = &Numbers[6][6];
for (int i=0; i<36; i++)
{
fwrite(ptr, sizeof(int), 36*36, Numfile);
}
fclose(Numfile);
//ofstream out("Numbers.txt");
}
Everything seems fine, I'm puzzled why the reference in the main says the array is undefined, any ideas?
edit: Apologies, missed some bits
Player.h
#pragma once
#include <iostream>
#include <fstream>
using namespace std;
class Player
{
private:
public:
Player();
~Player();
void Input();
int Numbers[6][6];
};
Player.cpp
#include <iostream>
#include <fstream>
#include "Player.h"
using namespace std;
Player::Player()
{
}
Player::~Player()
{
}
void Player::Input()
{
int num(0);
int duplicate = 0;
int game = 0;
int NumberofGames = 0;
cout<<"How many games do you want to play for this weeks draw?"<<endl;
cin>>NumberofGames;
if (NumberofGames>6)
{
cout<<"Please enter an amount between 1 and 6"<<endl;
cin>>NumberofGames;
}
do
{
for (int i=0;i<6;i++)
{
cout<<"Enter Number "<< (i+1) <<endl;
cin>>num;
if (num > 0 && num <67)
{
Numbers[game][i]= num;
}
else
{
cout <<"Please enter number between 1 and 66"<<endl;
i = i-1;
}
}
game = game + 1;
NumberofGames = NumberofGames - 1;
}
while (NumberofGames=0);
}
void PassInfo(int (&Numbers)[6][6]);
That line does not declare an array - it declares a function. You have no declaration for an array in your class (in fact, you have no data members declared in your class at all).
If you want to declare a member array, you need to modify your class definition:
class LotteryData
{
private:
int Numbers[6][6]; // this declares an array
public:
LotteryData();
~LotteryData();
void PassInfo(int (&arr)[6][6]); // this is still a function declaration
};
Just because you made a function's parameter be named Numbers doesn't magically mean that your program has an array called Numbers declared in it.
So a Player has an array called "Numbers".
Then you would use it like this:
data.PassInfo(player.Numbers);
In your main() function,
data.PassInfo(int (&Numbers)[6][6]);
This is wrong. You should simply pass a reference to 2D array.
int (&Numbers)[6][6];
data.PassInfo(Numbers);