Trying to create instance of class throws vtable error in XCode - c++

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

Related

Not Compiling When Using Pointer to Function

My code compiles and runs fine when I incorporate all of this as a single file. However, when I use a header file and use separate files, I get this error:
Undefined symbols for architecture x86_64:
"someClass::newNode()", referenced from:
_main in check.o
someClass::insert(someClass::Node*, char const*, char const*) in entry.o
ld: sym
bol(s) not found for architecture x86_64
I have tried everything and I cannot find what the issue is. They compile separately using "-c" but linking the object files gives me the error. Also, I am using inclusion guards and all the suggested tips when including header files. Any help would be appreciated!
//.h file
class someClass{
public:
//other stuff
struct Node
{
//...
};
Node *newNode();
};
//entry.C
Node someClass::newNode(){
someClass::Node *bNode = new someClass::Node;
//...
return bNode;
}
//check.C
int main(){
//...
someClass obj;
someClass.Node *root = obj.newNode();
return 0;
}
For getting nested type in c++ use "::" instead of "."
someClass::Node *root = obj.newNode();

Error message appearing on compiling g++ [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 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_;
}

Why virtual function has to be implemented in superclass?

I am trying to compile the following code:
#include <iostream>
class X{
public:
virtual void func();
};
class Y : public X{
public:
virtual void func(){
std::cout << "y" << std::endl;
}
};
int main(){
Y* y = new Y();
y->func();
return 0;
}
But building fails (on Xcode - C++11) with the following messages:
Undefined symbols for architecture x86_64:
"typeinfo for X", referenced from:
typeinfo for Y in c.o
"vtable for X", referenced from:
X::() in c.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
However, as soon as I add an implementation for func in X, it builds successfully. I am pretty sure, that virtual method is optional to be implemented in the superclass but I don't understand why is this happening. Also, if comment the code in main(), it builds successfully. I am assuming that the problem is calling the func() in main, but Xcode doesn't list it as runtime error, it only says build-time error.
If you don't want to implement the virtual function in the base class at all, simply mark it as pure virtual:
virtual void func() = 0;
No, you are wrong. You need to have implementations for non-pure virtual functions. If you do not want to provide an implementation, you need to make function pure virtual, using = 0 syntax.

Template inheritance in C++ and undefined symbols on Xcode

I have seen many related questions to this problem, but after carefully following advice from members, my problem still persists. The code is quite simple. I only have the following header file ("instrument.h"), which contains the base class and the template class:
#include <stdio.h>
#include <string>
using namespace std;
class Instrument
{
public:
Instrument();
virtual void print() const = 0;
};
template <class parameter> class Equity : public Instrument
{
public:
Equity();
virtual void print() const;
};
Now, in my main function on main.cpp I only do the following:
#include "instrument.h"
#include <iostream>
int main() {
Equity<double> pb;
return 0;
}
Well, I get the very well-known error:
Undefined symbols for architecture x86_64:
"Equity<double>::Equity()", referenced from:
_main in main.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 already changed in Build Settings the C++ standard library to libstdc++, also to default compiler, and so on. Do I have a problem with my project settings? Is perhaps the template wrongly implemented? I was thinking I should also have a instrument.cpp file, but then again definitions for templates must be kept in the header file so that would probably crash too.
Thanks in advance
You declared the default constructors for both Instrument and Equity but defined them nowhere.
Alter their definitions appropriately:
public:
Equity() = default; // Or {} in pre-C++11
// ^^^^^^^^^
(And equivalently for Instrument)
You can also completely omit the declarations of any default constructors for now since you didn't declare any other constructors in both Equity and Instrument and the default constructors will be generated automatically.

GCC Undefined symbols for architecture x86_64 in C++ Constructor

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.