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;
}
Related
This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Closed 2 years ago.
Sorry if it's a duplicate question, but I've been searching for solution since yesterday and I haven't found out yet..
I'm using CLion on mac, and my compiler is from xcode so i think it's clang. Any help would be appreciated!! Do I need to use gcc? or do I need to fix my cmake?
here's an error message.
[ 33%] Linking CXX executable bag
Undefined symbols for architecture x86_64:
"Bag<int>::add(int)", referenced from:
_main in main.cpp.o
"Bag<int>::print()", referenced from:
_main in main.cpp.o
"Bag<int>::Bag()", referenced from:
_main 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)
make[3]: *** [bag] Error 1
make[2]: *** [CMakeFiles/bag.dir/all] Error 2
make[1]: *** [CMakeFiles/bag.dir/rule] Error 2
make: *** [bag] Error 2
Here's main.cpp
#include <iostream>
#include "Bag.h"
int main() {
Bag<int> temp;
temp.add(1);
temp.print();
cout << endl;
return 0;
}
Here's Bag.h.
#ifndef BAG_BAG_H
#define BAG_BAG_H
#include <vector>
#include <cstdlib>
#include <iostream>
using namespace std;
static const size_t CAPACITY = 100;
template <class T>
class Bag{
public:
Bag();
size_t size() const;
bool empty();
bool check(const T& item);
void resize(size_t new_size);
void clear();
void remove(const T& item);
void add(T item);
void print();
private:
T* data;
size_t _size;
};
#endif //BAG_BAG_H
Here's Bag.cpp.
#include "Bag.h"
template <class T>
Bag<T>::Bag() : data{new T[CAPACITY]}, _size{0} {}
template <class T>
size_t Bag<T>::size() const {return _size;}
template <class T>
bool Bag<T>::empty() {
}
template <class T>
bool Bag<T>::check(const T &item) {
}
template <class T>
void Bag<T>::resize(size_t new_size) {
T* temp = new T[new_size];
for(int i = 0; i == _size; i++)
*data++ = *temp++;
delete [] data;
this->data = &temp;
}
template <class T>
void Bag<T>::add(T item) {
data[_size + 1] = item;
_size++;
}
template <class T>
void Bag<T>::print() {
for(int i = 0; i == _size; i++)
cout << data[i] << " ";
}
here's CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(bag)
set(CMAKE_CXX_STANDARD 14)
add_executable(bag main.cpp Bag.cpp Bag.h)
The issue is not you compiler or make system.
This is how templates work in C++:
Template code is compiled only when instantiated.
As a result, you can not separate template functions (or class members) in to a separate cpp file, instead you must keep everything in the header file.
Read the last paragraph here:
http://www.cplusplus.com/doc/oldtutorial/templates/
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.
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;
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;
}
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;