This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 4 years ago.
So for class we are learning about OOP in C++, and I built my class but every time I try to compile it I get this error message:
Undefined symbols for architecture x86_64:
"Player::set_assits(int)", referenced from:
_main in playerDataBase-666bbb.o
"Player::set_last_name(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
_main in playerDataBase-666bbb.o
"Player::set_team_name(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
_main in playerDataBase-666bbb.o
"Player::set_first_name(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
_main in playerDataBase-666bbb.o
"Player::set_year_of_birth(int)", referenced from:
_main in playerDataBase-666bbb.o
"Player::set_goals(int)", referenced from:
_main in playerDataBase-666bbb.o
"Player::Player()", referenced from:
_main in playerDataBase-666bbb.o
"Player::~Player()", referenced from:
_main in playerDataBase-666bbb.o
ld: symbol(s) not found for architecture x86_64
I can't understand why this happens, I have tried to run this by compiling separate .h, and .cpp files as well as by putting the class, and main function inside of the same .cpp file. Any help would be appreciated as well here is my code.
#include <iostream>
#include <string>
using namespace std;
class Player{
public:
Player();
~Player();
// accessors and mutators
void set_first_name(string in_first_name);
string first_name();
void set_last_name(string in_last_name);
string last_name();
void set_team_name(string in_team_name);
string team_name();
void set_year_of_birth(int in_year_of_birth);
int year_of_birth;
void set_goals(int in_goals);
int goals;
void set_assits(int in_assists);
int assists;
//methods
void display();
private:
string first_name_;
string last_name_;
string team_name_;
int year_of_birth_;
int goals_;
int assits_;
};
void Player::display(){
cout << first_name_ << last_name_ << endl;
}
int main(){
Player player;
player.set_first_name("John");
player.set_last_name("Tedesco");
player.set_team_name("Blyth Warriors");
player.set_year_of_birth(2002);
player.set_goals(2);
player.set_assits(7);
player.display();
}
Thanks for any help and sorry again for messy / poor code.
Thanks,
John
None of these methods are defined, only declared:
Player();
~Player();
// accessors and mutators
void set_first_name(string in_first_name);
string first_name();
void set_last_name(string in_last_name);
string last_name();
void set_team_name(string in_team_name);
string team_name();
void set_year_of_birth(int in_year_of_birth);
void set_goals(int in_goals);
void set_assits(int in_assists);
You need to provide reasonable definitions for these methods. Right now, you tell the compiler you will provide a definition for each of these methods, and never do. For default constructors, you may use Player() = default;, if you want the compiler to generate a constructor.
For example, to define the first_name property, you may do:
void Player::set_first_name(const string& in_first_name)
{
first_name_ = in_first_name;
}
const string& first_name() const
{
return first_name_;
}
Please note I've changed this to take value by constant reference, and return a value by constant reference, rather than by value, for efficiency reasons.
You declared all those fancy methods, e.g:
void set_first_name(string in_first_name);
string first_name();
But you never defined them!
Implement them, and it will work, e.g:
void set_first_name(string in_first_name) {
first_name_ = in_first_name;
}
string first_name() {
return first_name_;
}
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 6 years ago.
I am making a program with a list of baby names but I've decided to make a seperate function to open the file, this is what I have got so far.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void open_file(ifstream& in, char fileName[]);
void find_name(ifstream& in, string name, int numNames);
int main() {
const int NUMNAMES = 1000;
ifstream inStream;
char fileName[30];
string name;
cout << "Enter the name of the file that contains the names: " << endl;
open_file(inStream, fileName);
cout << "Enter the name to search for (capitalize first letter): " << endl;
cin >> name;
find_name(inStream, name, NUMNAMES);
inStream.close();
}
void open_file(ifstream& ) {
string line;
ifstream myfile ("babyNames.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout << line << '\n';
}
myfile.close();
}
else cout << "I/O failure opening file babyNames";
}
Does anyone know why I am getting so many error messages:
Undefined symbols for architecture x86_64:
"find_name(std::__1::basic_ifstream<char, std::__1::char_traits<char> >&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, int)", referenced from:
_main in Untitled-1b6d2e.o
"open_file(std::__1::basic_ifstream<char, std::__1::char_traits<char> >&, char*)", referenced from:
_main in Untitled-1b6d2e.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Does anyone know what I am doing wrong, I feel like it is relatively close I'm just fairly new to streams in c++.
The shown code declares and calls the following functions:
void open_file(ifstream& in, char fileName[]);
void find_name(ifstream& in, string name, int numNames);
Unfortunately, the shown code does not define any of these two functions, and the two linking errors are the result of that.
The shown code does define some function that's also called open_file(), but it's a completely different function because it takes different parameters. The shown code does not define any function called find_name().
You cannot simply declare a function like:
void open_file(ifstream& in, char fileName[]);
And then expect the code for this function to automatically appear somewhere. You have to define, and write the contents of this function. The parameters in this function, when you define it, must be the same as what you declared here.
I just started up a new project, and my class skeleton does not compile. The compiler error I am receiving is:
Undefined symbols for architecture x86_64:
"SQLComm::ip", referenced from:
SQLComm::SQLComm(int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in SQLComm.o
"SQLComm::port", referenced from:
SQLComm::SQLComm(int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in SQLComm.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I have no idea why my code does not compile... Here's the class which errors:
SQLComm.h:
#ifndef __WhisperServer__SQLComm__
#define __WhisperServer__SQLComm__
#include <iostream>
#include <string>
class SQLComm {
public:
//Local vars
static int port;
static std::string ip;
//Public functions
void connect();
SQLComm(int sqlport, std::string sqlip);
~SQLComm();
private:
};
#endif /* defined(__WhisperServer__SQLComm__) */
And here's the SQLComm.cpp:
#include "SQLComm.h"
SQLComm::SQLComm(int sqlport, std::string sqlip){
ip = sqlip;
port = sqlport;
}
SQLComm::~SQLComm(){
}
void SQLComm::connect(){
}
The system is OSX10.9, and the compiler is GCC (in xCode).
If anyone could tell me why I am getting this error, I'd be very happy. Thanks in advance! :)
You have declared static variables but you haven't defined them. You need to add this
int SQLComm::port;
std::string SQLComm::ip;
to your SQLComm.cpp file.
Although... thinking about it this is probably not what you intended. You intended to declare non-static member variables, e.g., each instance of SQLComm should contain those variables, right? In that case, simply drop the static (and don't add the above to your .cpp file.
You need to define your static class variables. Try
int SQLComm::port;
std::string SQLComm::ip;
in SQLComm.cpp.
Note: Most probably, you do not want to declare both variable as static class variables but as normal instance variables.
just wanted to start some C++ and created these simple class:
Ellipsoid.h
#ifndef __Ellipsoid__Ellipsoid__
#define __Ellipsoid__Ellipsoid__
#include <iostream>
#include <cassert>
class Ellipsoid {
private:
double axisA;
double flatteningF;
public:
Ellipsoid() {};
Ellipsoid(double aIn, double fIn);
double getAxisA();
double getFlatteningF();
};
#endif /* defined(__Ellipsoid__Ellipsoid__) */
Ellipsoid.cpp
#include "Ellipsoid.h"
Ellipsoid::Ellipsoid (double aIn, double fIn) : axisA(aIn), flatteningF(fIn) {};
int main() {
std::cout << "bla";
Ellipsoid el = Ellipsoid(44.3, 32);
double test = el.getAxisA();
return 0;
}
as you can see nothing special here. i'm using xcode on osx10.8.
But when i run the programm i come to this error:
Undefined symbols for architecture x86_64:
"Ellipsoid::getAxisA()", referenced from:
_main in Ellipsoid.o
ld: symbol(s) not found for architecture x86_64
and i really can't figure out whats wrong. tried to set the architecture to 32 bit but this won't work neither
The definition of the Ellipsoid::getAxisA() function is missing. You must define somewhere. Right now you only have a declaration, not a definition. The definition could look something like this:
double Ellipsoid::getAxisA() { return axisA; }
And would live in Ellipsoid.cpp.
I am trying to create an instance of a the following class:
#ifndef Sik_GameEntity_h
#define Sik_GameEntity_h
class GameEntity {
public:
~GameEntity(){};
void setup();
void update();
void draw();
void clear();
protected:
private:
};
#endif
Each of these methods (minus the deconstructor) are fleshed out in my .cpp file.
for ( int i = 0; i < nEntities; i++ )
{
GameEntity ent;
ent.setup();
entities.push_back(ent);
}
I'm creating an instance and inserting it into a vector of Game Entity objects. When I create an instance, I get the following error from XCode:]
Undefined symbols for architecture i386:
"GameEntity::GameEntity()", referenced from:
appCore::setup() in appCore.o
"GameEntity::~GameEntity()", referenced from:
appCore::setup() in appCore.o
std::vector<GameEntity, std::allocator<GameEntity> >::_M_insert_aux(__gnu_cxx::__normal_iterator<GameEntity*, std::vector<GameEntity, std::allocator<GameEntity> > >, GameEntity const&) in appCore.o
void std::_Destroy<GameEntity>(GameEntity*) in appCore.o
"vtable for GameEntity", referenced from:
GameEntity::GameEntity(GameEntity const&) in appCore.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture i386
I've tried adding a constructor, but it didn't make a difference, I still received the same errors. Is it obvious what I'm doing wrong in my class?
EDIT:
I forgot to mention, I cleaned my build.
The following NOTE and error means that you are trying to inline the constructor or setup method in your cpp file rather than the header file. Remove all inline keywords in your cpp file
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture i386
It seems like my two files, userinterface.h
#ifndef USERINTERFACE_H
#define USERINTERFACE_H
#include <string>
#include "vocabcollection.h"
namespace user_interface
{
//Finds a file
//
//Returns when user selects a file
std::string findFile();
//more comments followed by functions
}
#endif
and userinterface.cpp,
#include "userinterface.h"
using namespace std;
using namespace user_interface;
string findFile()
{
return "./";
}
//more placeholder implementations of such functions; void functions have nothing within
//the brackets
are giving me this slew of errors from the linker:
Undefined symbols for architecture x86_64:
make: Leaving directory `longdirectorypath'
"user_interface::showTestResults(int, int)", referenced from:
vocabCollection::test() in vocabcollection.o
"user_interface::get(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)", referenced from:
addNewCollection() in mainlogic.o
loadNewCollection() in mainlogic.o
"user_interface::findFile()", referenced from:
loadNewCollection() in mainlogic.o
"user_interface::displayMainMenu(std::vector<vocabCollection, std::allocator<vocabCollection> >)", referenced from:
mainlogic() in mainlogic.o
"user_interface::getUserAction()", referenced from:
mainlogic() in mainlogic.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [cheapassVocab.app/Contents/MacOS/cheapassVocab] Error 1
The process "/usr/bin/make" exited with code 2.
Error while building project cheapassVocab (target: Desktop)
When executing build step 'Make'
What's happening here?
In the header file, you declare the function findFile in the namespace user_interface. In the cpp file the free function findFile is defined. Yes, you are using namespace user_interface, but the compiler doesn't know that the findFile defined there belongs to namespace user_interface. The result of all this is that you've declared user_interface::findFile and defined ::findFile. When you call user_interface::findFile, the linker cannot find it, since there's only the free function findFile.
Easily solved - cpp file:
#include "userinterface.h"
using namespace std;
namespace user_interface
{
string findFile()
{
return "./";
}
}
You cannot implement findFile like that; it really has to go in the namespace:
namespace user_interface
{
string findFile()
{
return "./";
}
}
or:
string user_interface::findFile()
{
return "./";
}
The using directive is only for lookup, not for definitions - imagine what using namespace std; would do to all your function definitions otherwise!
You are defining findFile in the wrong namespace.
Either
std::string user_interface::findFile()
{
return "./";
}
or
namespace user_interface
{
std::string findFile()
{
return "./";
}
}
using does not affect where names are defined, it only affects how names are looked up.