Undefined symbols for architecture x86_64: (Class code and Header file) - c++

datagenerator.hpp
#ifndef STOCK_INDICATOR_SOURCE_PROJECT_DATAGENERATOR_H
#define STOCK_INDICATOR_SOURCE_PROJECT_DATAGENERATOR_H
#include <random>
class RandomNumberRange{
public:
RandomNumberRange(int low, int high);
double operator()();
private:
std::mt19937 random_engine_;
std::uniform_int_distribution<> distribution_;
};
void generate(std::vector<int> data, RandomNumberRange generator);
#endif
datagenerator.cpp
class RandomNumberRange
{
public:
RandomNumberRange(int low, int high)
: random_engine_{std::random_device{}()}
, distribution_{low, high}
{}
double operator()()
{
return distribution_(random_engine_);
}
private:
std::mt19937 random_engine_;
std::uniform_int_distribution<> distribution_;
};
void generate(std::vector<int> data, RandomNumberRange generator)
{
return std::generate(std::begin(data), std::end(data), generator);
}
And this is my main function
int main()
{
std::vector<int> volume(days);
generate(volume, RandomNumberRange(1, 100));
return 0;
}
I get this error :
Undefined symbols for architecture x86_64:
"RandomNumberRange::RandomNumberRange(int, int)", referenced from:
random_input(unsigned int const&) in main.cpp.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 want to populate a vector with random integers and include the code for this process in a separate file.
I think the problem is in the header file but I cannot figure out where.

Related

g++ ld: symbol(s) not found for architecture x86_64 - without more specific error message

I've been trying to solve this problem for hours and hours...
I have a header file, implementation file and a driver file.
HEADER:
class PhoneNumber
{
private:
const int MAXTEXTS;
static int live;
static int text; // number of total texts from all the phones.
string areaCode;
string exchange;
string line;
int nlive;
int ntext; // number of texts sent on this phone
public:
static int MaxPhones;
PhoneNumber();
PhoneNumber(string, string, string, int);
void inputPhoneNumber();
void displayPhoneNumber();
void sendText();
void dialNum();
int getLive();
int getText();
int getnLive();
int getnText();
static void addLive()
{
live++;
}
static void addText()
{
text++;
}
};
IMPLEMENTATION:
int PhoneNumber::getnLive()
{
return nlive;
}
int PhoneNumber::getnText()
{
return ntext;
}
int PhoneNumber::getLive()
{
return live;
}
int PhoneNumber::getText()
{
return text;
}
error message:
habins-mbp:CS2000 Habin$ g++ -o PhoneNumber PhoneNumber.cpp PhoneNumberDriver.cpp
Undefined symbols for architecture x86_64:
"PhoneNumber::live", referenced from:
PhoneNumber::getLive() in PhoneNumber-f64d4d.o
PhoneNumber::addLive() in PhoneNumber-f64d4d.o
"PhoneNumber::text", referenced from:
PhoneNumber::getText() in PhoneNumber-f64d4d.o
PhoneNumber::addText() in PhoneNumber-f64d4d.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
IF I use g++ -c, it compiles meaning that the code works. It seems like the static int live is giving me so much trouble.
Been trying to solve this for 10+ hours and to no avail. I'm about to snap my computer in half!
please help me
You should define all the static member variables in IMPLEMENTATION part.
int PhoneNumber::live;
int PhoneNumber::text;
int PhoneNumber::MaxPhones;

Undefined symbols for architecture x86_64: party::getArrival()

I have a header file for the party class called party.h:
#ifndef party_h
#define party_h
#include <iostream>
class party{
private:
int people;
int waitTime;
bool waiting;
int arrival;
public:
party();
party(int, int);
int getPeople();
void setPeople(int);
bool isWaiting();
void setWaiting(bool);
void setWaitTime(int);
int getwaitTime();
void setArrival(int);
int getArrival();
int getTotalTime(int);
void decrement();
};
#endif
And its implementation in party.cpp:
#include "party.h"
party::party(){}
party::party(int numPeople, int a){
people = numPeople;
arrival = a;
}
int party::getPeople(){
return people;
}
void party::setPeople(int p){
people = p;
}
bool party::isWaiting(){
return waiting;
}
void party::setWaiting(bool w){
waiting = w;
}
void party::setWaitTime(int t){
waitTime = t;
}
int party::getwaitTime(){
return waitTime;
}
void party::setArrival(int a){
arrival =a;
}
int party::getTotalTime(int current){
return (current-arrival);
}
Whenever I build the project I get the error message below,
Ld /Users/shade/Library/Developer/Xcode/DerivedData/ResSim-fvkhqxhiupiizxgffxqgoxgolsmv/Build/Products/Debug/ResSim normal x86_64 cd /Users/shade/Dropbox/School/Gwinnett_Tech/CIST_2362/final/ResSim setenv MACOSX_DEPLOYMENT_TARGET 10.8 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -L/Users/shade/Library/Developer/Xcode/DerivedData/ResSim-fvkhqxhiupiizxgffxqgoxgolsmv/Build/Products/Debug -F/Users/shade/Library/Developer/Xcode/DerivedData/ResSim-fvkhqxhiupiizxgffxqgoxgolsmv/Build/Products/Debug -filelist /Users/shade/Library/Developer/Xcode/DerivedData/ResSim-fvkhqxhiupiizxgffxqgoxgolsmv/Build/Intermediates/ResSim.build/Debug/ResSim.build/Objects-normal/x86_64/ResSim.LinkFileList -mmacosx-version-min=10.8 -stdlib=libstdc++ -o /Users/shade/Library/Developer/Xcode/DerivedData/ResSim-fvkhqxhiupiizxgffxqgoxgolsmv/Build/Products/Debug/ResSim
Undefined symbols for architecture x86_64: "party::getArrival()", referenced from: restaurant::startSim() in restaurant.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
"party::getArrival()", referenced from: Restaurant::startSim() in restaurant.o Symbol(s) not found for architecture x86_64 Linker command failed with exit code 1 (use -v to see invocation)
This is a new error message since it was working earlier today/this weekend in visual studio. But I've changed a decent amount of code since then, the code in the gist has been updated and is what I'm working with. I'm currently trying to get it to build in xcode so I can finish debugging/programming my project that is due tonight. Any help is greatly appreciated! Thanks!
You do not define getArrival in your party.cpp file. You probably want:
int party::getArrival(){
return arrival;
}

c++ compile error Undefined symbols for architecture x86_64

I wrote a class named UserScore:
//header
using namespace std;
class UserScore{
public:
UserScore(const int &user_id, const int &rating);
private:
int _user_id;
int _rating;
};
//cpp
#include "UserScore.h"
UserScore::UserScore (const int &user_id, const int &rating):
_user_id(user_id),
_rating(rating)
{
}
The compile command is:
g++ src/UserScore.cpp -o obj/UserScore.o
But why this simple thing won't compile?
The error is:
Undefined symbols for architecture x86_64:
"_main", referenced from:
start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
You need to include the -c option to the compilation line if you want to generate an object file, otherwise the compiler assumes you are trying to build an executable and will complain if you don't have a main() method (which is happening here).
To make an object file which you will link later to a code file that has a "main()" method in it you need to use
g++ -c src/UserScore.cpp -o obj/UserScore.o
Which is what I think you are trying to do.
Alternatively you just need to add a main function to your code and then you can make an executable.
//header
using namespace std;
class UserScore{
public:
UserScore(const int &user_id, const int &rating);
private:
int _user_id;
int _rating;
};
//cpp
#include "UserScore.h"
UserScore::UserScore (const int &user_id, const int &rating):
_user_id(user_id),
_rating(rating)
{
}
int main()
{
return 0;
}

Linking error for a naive singleton class in C++

My code is:
class cMySingleton{
private:
static bool bInstantiated;
int mInt;
cMySingleton(){
mInt=0;
}
public:
cMySingleton(int c){
if (bInstantiated){
cout << "you can only instantiated once";
}
else {
cMySingleton();
mInt=c;
}
}
};
int main () {
cMySingleton s(5);
cMySingleton t(6);
}
The linker keeps complaining:
Undefined symbols for architecture x86_64:
"cMySingleton::bInstantiated", referenced from:
cMySingleton::cMySingleton(int) 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)
What is going on? C++ novice here~~
you should initialize static field.
http://ideone.com/Y1huV
#include <iostream>
class cMySingleton{
private:
static bool bInstantiated;
int mInt;
cMySingleton(){
mInt=0;
}
public:
cMySingleton(int c){
if (bInstantiated){
std::cout << "you can only instantiated once";
}
else {
cMySingleton();
mInt=c;
}
}
};
bool cMySingleton::bInstantiated = true;
int main () {
cMySingleton s(5);
cMySingleton t(6);
}
More information you can be find here:
Static Data Member Initialization
there was also missing include and std:: around cout.
Initialize
static bool bInstantiated;
outside of cMySingleton
bool CMySingleton::bInstantiated;
Dont forget to initialize your static member outside of your class declaration in .cpp file:
bool cMySingleton::bInstantiated = false;

C++: Having trouble using map with a static variable in a class, I get undefined symbol

This is the error I get
Undefined symbols:
"Config::fmap", referenced from:
register_function(unsigned int (*)(unsigned int))in Config.o
print_config() in shared.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
Config.h
#include <map>
#include <boost/preprocessor/stringize.hpp>
typedef unsigned int (*fptr_t)(unsigned int);
typedef std::map<fptr_t, std::string> function_name_map_type;
void register_hash_names();
void register_function(fptr_t funct_pointer);
class Config
{
public:
static function_name_map_type fmap;
};
Config.cpp
function_map_t fmap;
void register_hash_names()
{
register_function(sha1);
...
}
void register_function(fptr_t funct_pointer)
{
(Config::fmap)[funct_pointer] = BOOST_PP_STRINGIZE(funct_pointer);
}
Shared.cpp where the error originates from:
std::cout << "\t " << Config::fmap[Config::current_hash_function] << "\n";
The definition in the .cpp should be:
function_map_t Config::fmap;