ld: symbol(s) not found for architecture x86_64 Row::AddColumn - c++

Building target: Lab1
Invoking: MacOS X C++ Linker
g++ -o "Lab1" ./src/EclipseR.o ./src/EclipseR_test.o ./src/Row.o ./src/Rows.o
Undefined symbols for architecture x86_64:
"Row::AddColumn(int, std::__1::basic_string, std::__1::allocator >)", referenced from:
_main in EclipseR.o
Row.h
#fndef ROW_H_
#define ROW_H_
#include <iostream>
using namespace std;
Class Row {
public:
Row();
Row(std::string id);
string id;
string columns[24]
void AddColumn(int index, std::string value);
};
#endif
Row.cpp
#include "Row.h"
#include <iostream>
#include <string>
using namespace std;
string columns[24];
Row::Row(){
// TODO
}
Row::Row(string id) {
this->id = id;
cout << "ID: " << this->id;
}
Row::~Row()
{
//Deconstructor
}
void AddColumn(int index, std::string value)
{
columns[index] = value;
}

I figured out my issue.
in my Row.cpp class I need to change the following
void AddColumn(int index, std::string value){...}
to
void Row::AddColumn(int index, std::string value){...}

Related

VScode on MAC: linker command failed with exit code

I am new to C++ using vscode on MAC and I tried to test using a sample program.
In the header file, I declare the class.
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
using namespace std;
class Student{
public:
string name;
int rollno;
Student();
Student(string s, int id);
void printDetails();
};
#endif
The implementation is done in the .cpp file.
#include <iostream>
#include "student.h"
using namespace std;
Student::Student(){}
Student::Student(string s, int id):name(s),rollno(id){}
void Student::printDetails(){
cout<< rollno << "-" << name<<endl;
}
Finally, I added Main.cpp.
#include <iostream>
#include "student.h"
using namespace std;
int main(){
Student students[8];
students[0] = Student("Tom", 1);
students[1] = Student("Jerry", 2);
for(int i = 0; i < 2; i++){
students[i].printDetails();
}
}
However, when I followed the procedures suggested in https://code.visualstudio.com/docs/cpp/config-clang-mac . The compiler issued errors
Undefined symbols for architecture x86_64:
"Student::printDetails()", referenced from:
_main in Main-896002.o
"Student::Student(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, int)", referenced from:
_main in Main-896002.o
"Student::Student()", referenced from:
_main in Main-896002.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The issue can be resolved by manually linking Main.cpp with student.cpp, i.e.,
g++ Main.cpp student.cpp -o Main
However, is there a more effective way to do so as the manual work is troublesome when the project size is large?

C++ Apple Mach-O Linker Error/Symbols "::" Not Found for Architecture

I have the following header file (Student.h)
#ifndef STUDENT_H_
#define STUDENT_H_
#include <string>
using namespace std;
class Student {
int id;
string name;
string school;
public:
/*
* Default ctor
*/
Student();
/*
* Overloaded ctor
*/
Student(int id, string name, string school);
/*
* Compares two students by ID for equality
*/
bool operator == (const Student& other) const;
/*
* Compares two students by ID for ordering
*/
bool operator < (const Student& other) const;
};
#endif /* STUDENT_H_ */
And here is my implementation (in another file called Student.cpp):
#include "Student.h"
#include <string>
using namespace std;
bool Student::operator==(const Student& other) const{
return(Student::id == other.id);
}
bool Student::operator<(const Student& other) const{
return(Student::id < other.id);
}
And then finally a main.cpp file which references both files:
#include "Student.h"
#include "Student.cpp"
#include <iostream>
using namespace std;
int main() {
bool equal;
Student s1(111, "Jeff", "Rockywood High");
Student s2(100, "Bobby", "Carmel High");
equal = (s1==s2);
cout << equal;
}
I am getting an error from xcode telling me that:
the Undefined symbols for architecture x86_64:
Student::Student(int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
By placing #include "Student.cpp" in the Student.cpp file I had included the same file twice, without realizing it. Thus, Student::Student was defined twice...and thus the duplicate symbol error.

How to correctly instantiate a (has-a) class? Undefined symbols?

So I'm new to C++, I was mainly reading out of a book, but after a while I realized it was pretty vague. Currently just running through some of my knowledge with some examples,but I'm currently pulling out my hair with this bug, I've tried retracing my steps and starting from scratch twice, and end up with the same issue.
Problem: I have a vehicle class, that has an Engine and a Tank, but when compiling I get an error of undefined symbols.
Xcode error:
Undefined symbols for architecture x86_64: "Tank::Tank(float)", referenced from:
Vehicle::Vehicle(int, float, int) in Vehicle.o "Tank::~Tank()", referenced from:
Vehicle::Vehicle(int, float, int) in Vehicle.o "Engine::Engine(int)", referenced from:
Vehicle::Vehicle(int, float, int) in Vehicle.o "Engine::~Engine()", referenced from:
Vehicle::Vehicle(int, float, int) in Vehicle.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
Given Example: Building a car
Folder Structure:
Tank/
Tank.h
Tank.cpp
Engine/
Engine.h
Engine.cpp
Vehicle.h
vehicle.cpp
main.cpp
Tank.h:
#ifndef __Test__Tank__
#define __Test__Tank__
class Tank
{
public:
Tank (float);
virtual ~Tank();
protected:
const float tankCapacity;
};
#endif
Tank.cpp:
#include <iostream>
#include "Tank.h"
using namespace std;
//Implementation of constructor
Tank::Tank(float capacity) : tankCapacity(capacity){
cout << "Tank was created...with a tank capacity of " << this->getTankCapacity()<< endl;
}
//Implementation of destructor
Tank::~Tank(){
cout << "Tank was destroyed..." << endl;
}
Engine.h:
#ifndef __Test__Engine__
#define __Test__Engine__
#include <iostream>
#include "../Tank/Tank.h"
class Engine
{
public:
Engine(int);
~Engine();
protected:
bool started;
int trip, numCyl;
};
#endif /* defined(__Test__Engine__) */
Engine.cpp:
#include <iostream>
#include "Engine.h"
using namespace std;
Engine::Engine(int num) : numCyl(num)
{
this->started = false;
this->trip = 0;
cout << "Engine was created with " << this->numCyl << " cylinders" << endl;
}
Engine::~Engine(){
cout << "Engine was destroyed" << endl;
}
Vehicle.h:
#ifndef __Test__Vehicle__
#define __Test__Vehicle__
#include <iostream>
#include "Engine/Engine.h"
#include "Tank/Tank.h"
class Vehicle
{
public:
~Vehicle();
protected:
Vehicle(int occ, float tankCapacity, int numCyl);
int occupants;
Engine engine;
Tank tank;
private:
Vehicle();
};
#endif /* defined(__Test__Vehicle__) */
Vehicle.cpp:
#include <iostream>
#include "Tank/Tank.h"
#include "Engine/Engine.h"
#include "Vehicle.h"
using namespace std;
Vehicle::Vehicle(int occ, float tankCapacity, int numCyl) : engine(numCyl), tank(tankCapacity)
{
this->occupants = occ;
cout << "Vehicle Created" << endl;
}
Thank you for any help that comes my way, any small hint would be appreciated!

Error after reorganization modulaire

I transformed my program in a modular program becahse all was in one header.
But now there is a problem I don't know ..
Undefined symbols for architecture x86_64:
"Vehicule::createVehicule(std::string)", referenced from:
_main in main-aV2PNG.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: *** [main] Error 1
main.cpp
#include <iostream>
#include "Vehicule.h"
int main()
{
Vehicule *v=Vehicule::createVehicule("fr");
presenter(v);
return 0;
}
vehicule.h
#include <iostream>
class Vehicule
{
public:
virtual std::string getMarque() const =0;
friend std::ostream &operator<<(std::ostream &o,const Vehicule *v);
static Vehicule* createVehicule(std::string origine);
};
void presenter(const Vehicule *v)
{
std::cout << "Vehicule " << v << std::endl;
}
std::ostream &operator<<(std::ostream &o,const Vehicule *v)
{
return o << v->getMarque();
}
vehicule.cpp
#include "Vehicule.h"
#include "CreateurConcretRenault.h"
#include "CreateurConcretFiat.h"
Vehicule* Vehicule::createVehicule(std::string origine)
{
if(origine=="fr") return new CreateurConcretRenault();
else if(origine=="ita") return new CreateurConcretFiat();
else return new CreateurConcretRenault();
}
CreateurConcretRenault.h
#include <iostream>
#include "Vehicule.h"
class CreateurConcretRenault : public Vehicule
{
public:
std::string getMarque() const;
};
CreateurConcretRenault.cpp
#include "CreateurConcretRenault.h"
std::string CreateurConcretRenault::getMarque() const
{
return "Renault";
}
CreateurConcretFiat.h
#include <iostream>
#include "Vehicule.h"
class CreateurConcretFiat : public Vehicule
{
public:
std::string getMarque() const;
};
CreateurConcretFiat.cpp
#include "CreateurConcretFiat.h"
std::string CreateurConcretFiat::getMarque() const
{
return "Fiat";
}
You are using std::string, but you never included <string>.
I tried to put all in one header and it works ..
So I thought i forgot something

Getting symbols error: 'ld: symbol(s) not found'

I'm trying to execute this code, but I'm getting the symbol error of the title:
configfile.cpp:
#include "configFile.h"
#include <iostream>
#include <sstream>
#include <fstream>
#include <cstdlib>
#include <stdexcept>
#include <map>
configFile* configFile::getInstance(){
pinstance = new configFile();
return pinstance;
}
configFile::configFile(){
filename = "/Users/myfolder/NetBeansProjects/Oier_2/config.cfg";
}
void configFile::setConfigFileName(std::string s){
filename = s;
}
float* getConfiguration(std::string type, int size) {
std::string data[size];
std::string line;
std::ifstream myfile("/Users/myfolder/NetBeansProjects/Oier_2/config.cfg");
while (std::getline(myfile, line)) {
std::istringstream is_line(line);
std::string key;
if (std::getline(is_line, key, '=')) {
if(key.compare(type) == 0){
for(int i=0; i<size;i++){
std::getline(is_line,data[i],',');
}
}
}
}
float *fdata;
for(int i=0;i<size;i++){
fdata[i] = (float)atof(data[i].c_str());
}
return fdata;
}
And configFile.h:
#include <string>
#ifndef CONFIGFILE_H
#define CONFIGFILE_H
class configFile {
private:
static configFile* pinstance;
static std::string filename;
public:
static configFile* getInstance();
void setConfigFileName(std::string s);
float* getConfiguration(std::string type, int size);
protected:
configFile();
configFile(const configFile& orig);
};
#endif /* CONFIGFILE_H */
The symbols error I'm having:
Undefined symbols: "configFile::filename", referenced from:
configFile::configFile()in configFile.o
configFile::configFile()in configFile.o
configFile::setConfigFileName(std::basic_string, std::allocator >)in configFile.o
"configFile::pinstance", referenced from:
configFile::getInstance() in configFile.o
configFile::getInstance() in configFile.o "configFile::getConfiguration(std::basic_string
std::char_traits, std::allocator >, int)", referenced
from:
_main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
If it's necessary: main.cpp:
#include <cstdlib>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <algorithm>
#include "configFile.h"
using namespace std;
int main(int argc, char** argv) {
configFile* cfg = configFile::getInstance();
string type = "tiempo";
float* tiem = cfg->getConfiguration(type,3);
for(int i=0; i< 3;i++){
printf( " %f ", tiem[i]);
}
}
I'm running a MaxOSX 10.6.8. Thanks in adavance
These two areas are the issue:
configFile::configFile(){
filename = "/Users/myfolder/NetBeansProjects/Oier_2/config.cfg";
}
void configFile::setConfigFileName(std::string s){
filename = s;
}
You've declared filename as a static data member, so its name must always be configFile::filename. If you meant to use a data member, you can simply remove the static definition.
The difference between static and non-static data members is that with static, only one variable exists, whereas with a non-static data member there is an instance of this variable per-class. So in this case, the consequence is that with static, each instance of configFile would reference the same file path, whereas removing static would make each configFile own its own filename and so have its own file path.