Reputation: 149
For some reason, i get a linker error. my distance.cpp compiles perfectly however same cannot be said for main.cpp this is the error i get:
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Users\Himalaya\Desktop\C++\testdistance\main.cpp" -o "C:\Users\Himalaya\Desktop\C++\testdistance\main.exe" -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0x13b):main.cpp: undefined reference to `Distance::Distance()'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0x150):main.cpp: undefined reference to `Distance::setInches(double)'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0x16d):main.cpp: undefined reference to `Distance::Distance(int, double)'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0x18a):main.cpp: undefined reference to `Distance::Distance(int, double)'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0x19c):main.cpp: undefined reference to `Distance::Distance(Distance const&)'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0x1af):main.cpp: undefined reference to `Distance::setFeet(int)'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0x1c6):main.cpp: undefined reference to `Distance::Distance(Distance const&)'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0x1d8):main.cpp: undefined reference to `Distance::Distance(Distance const&)'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0x1ea):main.cpp: undefined reference to `Distance::Distance(Distance const&)'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0x1fc):main.cpp: undefined reference to `Distance::Distance(Distance const&)'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0x3e1):main.cpp: undefined reference to `Distance::Distance(Distance const&)'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0x3fc):main.cpp: more undefined references to `Distance::Distance(Distance const&)' follow
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0x52a):main.cpp: undefined reference to `Distance::Distance()'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0x54c):main.cpp: undefined reference to `Distance::operator+(Distance const&)'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0x598):main.cpp: undefined reference to `Distance::showDistance()'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0x5c0):main.cpp: undefined reference to `Distance::showDistance()'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0x5e8):main.cpp: undefined reference to `Distance::showDistance()'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0x7f8):main.cpp: undefined reference to `Distance::Distance()'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0x81a):main.cpp: undefined reference to `Distance::operator-(Distance const&)'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0x866):main.cpp: undefined reference to `Distance::showDistance()'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0x88e):main.cpp: undefined reference to `Distance::showDistance()'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0x8b6):main.cpp: undefined reference to `Distance::showDistance()'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0xac7):main.cpp: undefined reference to `Distance::Distance()'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0xb02):main.cpp: undefined reference to `Distance::operator+(Distance const&)'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0xb97):main.cpp: undefined reference to `Distance::showDistance()'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0xc3a):main.cpp: undefined reference to `Distance::showDistance()'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0xdc9):main.cpp: undefined reference to `Distance::showDistance()'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0xe9f):main.cpp: undefined reference to `Distance::getInches()'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0xeb6):main.cpp: undefined reference to `Distance::getFeet()'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0xfcd):main.cpp: undefined reference to `Distance::Distance()'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0xfd8):main.cpp: undefined reference to `Distance::Distance()'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0xfe3):main.cpp: undefined reference to `Distance::getInches()'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0xff4):main.cpp: undefined reference to `Distance::getFeet()'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0x101b):main.cpp: undefined reference to `Distance::getInches()'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0x102c):main.cpp: undefined reference to `Distance::getFeet()'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0x105d):main.cpp: undefined reference to `Distance::setInches(double)'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0x107a):main.cpp: undefined reference to `Distance::setInches(double)'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0x10a0):main.cpp: undefined reference to `Distance::showDistance()'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0x10c8):main.cpp: undefined reference to `Distance::showDistance()'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0x10f0):main.cpp: undefined reference to `Distance::showDistance()'
C:\Users\Himalaya\AppData\Local\Temp/cckxbaaa.o(.text+0x111b):main.cpp: undefined reference to `Distance::showDistance()'
collect2: ld returned 1 exit status
Execution terminated
Here is my codes;
distance.cpp
#include <cmath>
#include <sstream>
#include <string>
#include "distance.h"
using namespace std;
Distance::Distance()
{
feet = 0;
inches = 0;
}
Distance::Distance(int ft, double in)
{
this->feet = ft;
if(in >= 12)
{
while(in >= 12) // pull as many feet as possible out of inches
{
this->feet += 1;
in -= 12;
}
this->inches = in; // store remainder
}
else
{
this->inches = in;
}
}
Distance::Distance(const Distance &dist)
{
this->feet = dist.feet;
this->inches = dist.inches;
}
void Distance::setInches(double in)
{
if(in >= 12)
{
while(in >= 12) // pull as many feet as possible out of inches
{
this->feet += 1;
in -= 12;
}
this->inches = in; // store remainder
}
else
{
this->inches = in;
}
}
double Distance::getInches()
{
return this->inches;
}
void Distance::setFeet(int ft)
{
this->feet = ft;
}
int Distance::getFeet()
{
return this->feet;
}
bool Distance::operator==(Distance &dist)
{
if((this->feet == dist.feet) && (this->inches == dist.inches))
{
return true;
}
else
{
return false;
}
}
Distance Distance::operator+(const Distance &dist)
{
Distance dist_return;
double total_inches = 0;
dist_return.feet = this->feet + dist.feet;
total_inches = this->inches + dist.inches;
if(total_inches >= 12)
{
while(total_inches >= 12) // pull as many feet as possible out of inches
{
dist_return.feet += 1;
total_inches -= 12;
}
dist_return.inches = total_inches;
}
else
{
dist_return.inches = this->inches + dist.inches;
}
return dist_return;
}
Distance Distance::operator-(const Distance &dist)
{
Distance dist_return;
int temp_feet = this->getFeet(); // copy "this" so we can do math and show properly
double temp_inches = this->getInches();
dist_return.feet = temp_feet - dist.feet;
dist_return.inches = temp_inches - dist.inches;
dist_return.feet = abs(dist_return.feet);
dist_return.inches = abs(dist_return.inches); // distance is never negative, and - means difference, so 12 - 20 = 8
return dist_return;
}
string Distance::showDistance()
{
string str_return = "";
std::ostringstream temp_str_holder;
if(this->feet > 0) // only print feet if we have any
{
temp_str_holder.str(""); // clear out ostringstream
temp_str_holder << this->feet;
str_return += temp_str_holder.str();
if(this->feet == 1)
{
str_return += " foot";
}
else
{
str_return += " feet";
}
}
if(this->inches > 0) // only print inches if we have any
{
if(this->feet > 0)
{
str_return += " "; // if we have both feet and inches, print spacer between terms
}
temp_str_holder.str(""); // clear out ostringstream
temp_str_holder << this->inches;
str_return += temp_str_holder.str();
if(this->inches == 1)
{
str_return += " inch";
}
else
{
str_return += " inches";
}
}
if((this->feet == 0) && (this->inches == 0)) // if 0 feet 0 inches, just say 0 inches
{
str_return = "0 inches";
}
return str_return;
}
distance.h
#ifndef DISTANCE_H
#define DISTANCE_H
#include <cmath>
#include <sstream>
#include <string>
using namespace std;
class Distance
{
private:
double inches;
int feet;
public:
Distance();
Distance(int ft, double in);
Distance(const Distance &dist);
void setInches(double in);
double getInches();
void setFeet(int ft);
int getFeet();
bool operator==(Distance &dist);
Distance operator+(const Distance &dist);
Distance operator-(const Distance &dist);
string showDistance();
};
#endif // DISTANCE_H
and my main.cpp which the program actually suppose to run from, however it gives me that weird error stated above.
#include <iostream>
#include <iomanip>
#include <string>
#include "distance.h"
using namespace std;
void addDistances(Distance dist1, Distance dist2);
void subtractDistances(Distance dist1, Distance dist2);
void sumDistanceArray(Distance dist_array[]);
void plotDistanceArray(Distance dist_array[]);
void calcRectangle(Distance dist1, Distance dist2);
//void calcVolume(Distance dist1, Distance dist2);
int main()
{
Distance dist1; // showing initialization using defaults
dist1.setInches(6.7);
Distance dist2(1,7); // showing initialization to a distance
Distance dist3(0,34); // showing the auto-conversion of inches to feet as needed
Distance dist4(dist3); // showing initialization to another distance object's values
dist4.setFeet(4);
Distance dist_array[4] = {dist1, dist2, dist3, dist4};
int menu_entry;
bool menu_exit = false;
do
{
//display menu
cout << endl << endl << endl;
cout << "***** Main Menu *****" << endl;
cout << "1: Add two distances" << endl;
cout << "2: Subtract two distances" << endl;
cout << "3: Sum an array of distances" << endl;
cout << "4: Plot an array of distances" << endl;
cout << "5: Calculate the perimeter and area and volume" << endl;
cout << "6: Exit" << endl;
cout << "Please enter the number of your selection: ";
cin >> menu_entry;
// check entry, warn and promt again if invalid
if ((menu_entry < 1) || (menu_entry > 6))
{
cout << endl;
cout << "Invalid entry! Please try again." << endl;
continue;
}
switch (menu_entry)
{
case 1:
addDistances(dist2, dist3);
break;
case 2:
subtractDistances(dist2, dist3);
break;
case 3:
sumDistanceArray(dist_array);
break;
case 4:
plotDistanceArray(dist_array);
break;
case 5:
calcRectangle(dist2, dist3);
break;
case 6:
default:
menu_exit = true;
break;
}
}
while(!menu_exit);
return 0;
}
void addDistances(Distance dist1, Distance dist2)
{
Distance dist_total;
dist_total = dist1 + dist2;
cout << endl;
cout << "The sum of " << dist1.showDistance() << " and " << dist2.showDistance() << " is "
<< dist_total.showDistance() << endl;
}
void subtractDistances(Distance dist1, Distance dist2)
{
Distance dist_total;
dist_total = dist1 - dist2;
cout << endl;
cout << "The difference of " << dist1.showDistance() << " and " << dist2.showDistance() << " is "
<< dist_total.showDistance() << endl;
}
void sumDistanceArray(Distance dist_array[])
{
int i;
int num_distances = 4;
Distance dist_total;
for(i=0; i<num_distances; i++)
{
dist_total = dist_total + dist_array[i];
}
cout << endl;
cout << "The sum of: " << endl;
for(i=0; i<num_distances; i++)
{
cout << dist_array[i].showDistance() << endl;
}
cout << "is " << dist_total.showDistance() << endl;
}
void plotDistanceArray(Distance dist_array[])
{
int i;
int j;
double inches;
double bar_percentage;
double bar_length;
int num_distances = 4;
int num_feet_max = 6;
int num_max_disp_columns = 60;
cout << endl;
cout << "Plot of the array of distances : " << endl;
for(i=0; i<num_distances; i++)
{
cout << setw(20) << dist_array[i].showDistance() << " ";
inches = dist_array[i].getInches() + (12 * dist_array[i].getFeet());
bar_percentage = inches / (num_feet_max*12);
bar_length = bar_percentage * num_max_disp_columns; // calculate bar length
for(j=0; j<(bar_length+1); j++)
{
cout << "#"; // draw bar
}
cout << endl;
}
}
void calcRectangle(Distance dist1, Distance dist2)
{
Distance rect_area;
Distance rect_perimeter;
double inches1;
double inches2;
inches1 = dist1.getInches() + (12 * dist1.getFeet());
inches2 = dist2.getInches() + (12 * dist2.getFeet());
rect_area.setInches(inches1 * inches2);
rect_perimeter.setInches((2 * inches1) + (2 * inches2));
cout << endl;
cout << "A rectangle with dimensions " << dist1.showDistance() << " and " << dist2.showDistance() << " has an area of "
<< rect_area.showDistance() << " and a perimeter of " << rect_perimeter.showDistance() << endl;
}
what is the problem? what does cckxbaaa.o(.text+0x13b) mean?? This is something i have not encountered before that is why i am seeking for your help. All these 3 files are in the same folder/path, why does it giving me this error?
Upvotes: 0
Views: 2057
Reputation: 866
The linker does not seem to link the compiled object of distance.cpp. So distance.o is missing under the -o options
Check this and compare your linker call
http://homepages.gac.edu/~mc38/2001J/documentation/g++.html
(Another small issue, you do not need to include cmath, sstream and string in the distance.cpp file.
Upvotes: 0
Reputation: 76245
Don't worry about the meaning of cckxbaaa.o(.text+0x13b)
: that's hackerhead stuff. The important part is the rest of the message: "undefined reference to Distance::Distance()
" and the rest. It means that the linker hasn't seen the object file that has the Distance
member functions in it.
Unlike Java, C++ doesn't hunt around your folders to try to figure out what you wanted to use. To compile and link the executable:
g++ main.cpp distance.cpp
That compresses two steps into one. Step 1: compile each source file into an object file. Step 2: link the object files and the standard library together to produce an executable file. It's that last step that's failing, presumably because the linker command line isn't correct.
Upvotes: 1