Reputation: 21
I'm trying to access methods in my Element structure in my main method. I keep getting an error stating that
"Undefined symbols for architecture x86_64:
"Element::Element()", referenced from:
_main in Element.o
_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)"
Element header
#ifndef Element_hpp
#define Element_hpp
struct Element {
public:
int getNumber(void);
void setNumber(int n);
int getSteps();
void setSteps();
Element();
private:
int number;
int stepN;
};
#endif /* Element_hpp */
Element class
#include "Element.hpp"
#include "main.cpp"
int Element::getNumber() {
return number;
}
void Element::setNumber(int n) {
number = n;
setSteps();
}
int Element::getSteps() {
return stepN;
}
int steps(int n) {
int i = 0;
int j = n;
while(j != 1) {
if(j % 2 == 1) {
j = (3 * j + 1) / 2;
i = i + 2;
}
else if(j % 2 == 0) {
j= j / 2;
i= i + 1;
}
}
return i;
}
void Element::setSteps() {
stepN = steps(number);
}
Main class
#include <iostream>
#include <String>
#include <fstream>
#include "Element.hpp"
int main(int argc, const char * argv[]) {
std::ofstream data;
data.open("data.txt");
int end = 1000;
Element* e = new Element[end];
for(int i = 0; i < end; i++) {
e[i].setNumber(i);
e[i].setSteps();
}
heapSort(e, end);
printArray(e, end);
data.close();
return 0;
}
Upvotes: 1
Views: 76
Reputation: 1395
You have declared Element();
constructor in Element header file but you have not defined it in any file.
You may want to add this in your Element implementation file:
Element::Element() {
}
Upvotes: 1
Reputation: 2052
As Sam Varshavchik mentioned in the comment, you declared
Element::Element()
on your Element header file but did not define it. Adding
Element::Element() {
}
to your Element class file should allow the linker to find the missing symbol.
Upvotes: 1
Reputation: 83557
You should never #include CPP files. Instead you should link them. The most straight forward way is from the command line:
$ g++ main.cpp element.cpp
I suggest that you read about the C++ build process including the difference between compiling and linking.
Upvotes: 1