Help with Programming

Hi, So below are 3 files I have for a multifile project and i keep getting an error of:

Undefined symbols for architecture x86_64: "_main", referenced from: implicit entry/start for main executable ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

(1)fraction.h

#include<iostream>

using namespace std;

class fraction{

int num,deno;

public:

fraction();

fraction(int ,int );

fraction AddedTo(fraction);

fraction Subtract(fraction);

fraction MultipliedBy(const fraction);

fraction DividedBy(const fraction);

bool isGreaterThan(fraction);

bool isEqualTo(fraction);

void print();

};

(2)fraction.cpp

#include"fraction.h"

fraction::fraction(){

num=0;

deno=1;

}

fraction::fraction(int n,int d ){

num=n;

deno=d;

}

fraction fraction:: AddedTo(fraction f){

fraction val;

val.num=this->num*f.deno+f.num*this->deno;

val.deno=this->deno*f.deno;

return val;

}

fraction fraction::Subtract(fraction f){

fraction val;

val.num=this->num*f.deno-f.num*this->deno;

val.deno=this->deno*f.deno;

return val;

}

fraction fraction::MultipliedBy(const fraction f){

fraction val;

val.num=this->num*f.num;

val.deno=this->deno*f.deno;

return val;

}

fraction fraction::DividedBy(const fraction f){

fraction val;

val.num=this->num*f.deno;

val.deno=this->deno*f.num;

return val;

}

bool fraction::isGreaterThan(fraction f){

if(this->num*f.deno>this->deno*f.num)return true;

return false;

}

bool fraction::isEqualTo(fraction f){

if(this->num*f.deno==this->deno*f.num)return true;

return false;

}

void fraction::print(){

int a=this->num,b=this->deno,common_factor;

while (1){

if(a==0||b==0){

common_factor=0;

break;

}

if (a == b){

common_factor=a;

break;

}



if (a > b)a-=b;

else b-=a;

}

if(common_factor!=0){

cout<<this->num/common_factor<<"/"<<this->deno/common_factor<<"\n";

}

else cout<<this->num<<"/"<<this->deno<<"\n";

}

(3)client.cpp

#include <iostream>
#include "fraction.cpp"
using namespace std;

int main()
{
fraction f1(9,8); //calling a parameterized class constructor
fraction f2(2,3); //calling a parameterized class constructor
fraction result; //calling a default class constructor
fraction f3; //calling a default class constructor

cout << "The result starts off at ";
result.print(); //calling an observer function
cout << endl;

cout << "The product of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.MultipliedBy(f2); //a class binary operation - function
result.print();
cout << endl;

f3 = result; //assignment

if (f2.isGreaterThan(f3)){ //a class relational expression - boolean operation/function
f2.print();
cout <<" is greater than ";
f3.print();
cout<<endl;
} else {
f2.print();
cout <<" is less than ";
f3.print();
cout<<endl;
}

cout << "The sum of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.AddedTo(f2); //a class binary operation - function
result.print();
cout << endl;

cout << "The difference of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.Subtract(f2); //a class binary operation - function
result.print();
cout << endl;

if (f1.isEqualTo(f2)){ //a class relational expression - boolean operation/function
cout << "The two fractions are equal." << endl;
} else {
cout << "The two fractions are not equal." << endl;
}

fraction f4(12, 8);
fraction f5(202, 303);

result = f4.DividedBy(f5); //a class binary operation - function
cout << "The quotient of ";
f4.print();
cout << " and ";
f5.print();
cout << " is ";
result.print();
cout << endl;

system ("PAUSE");//exclude statement if not using Dev-C++
return 0;
}
You shouldn't include fraction.cpp in client.cpp. Just include fraction.h. If you still have problems then you need to say how you are compiling it.
Undefined symbols for architecture x86_64: "_main", referenced from: implicit entry/start for main executable


Undefined Symbol is a linker error, and in your case it is for the function "main"

either you didn't compile client.cpp or you didnt include client.o/.obj file in the link command.

and as tpb says, dont include cpp files.

fraction.cpp and client.cpp need to include "fraction.h"

both client.cpp and fraction.cpp need compiling.

the output of both of those compiles (.o/.obj files) need specifying to the linker

for gcc systems that would be....
1
2
3
g++ -c fraction.cpp
g++ -c client.cpp
g++ -o myexecutable fraction.o client.o


I dont understand what you @jaybob66 mean by include the "fraction.h" I am new to programming and need a bit more specific help on this topic. Right now under my project folder I have the three files so it goes
Project Name
-fraction.h
Fraction folder
main.cpp
Client folder
main.cpp
Products folder.
Also what do you mean by not including the fraction.cpp in client.cpp. They are in different files
I dont understand what you @jaybob66 mean by include the "fraction.h"

It means you need to use a #include statement, to include "fraction.h"

Also what do you mean by not including the fraction.cpp in client.cpp. They are in different files

Your code for Client.cpp contains the following line:

#include "fraction.cpp"
Last edited on
My code for client has:

#include <iostream>
#include "fraction.h"
using namespace std;

int main()
{
fraction f1(9,8); //calling a parameterized class constructor
fraction f2(2,3);
fraction result; //calling a default class constructor
const fraction f3(12, 8);
const fraction f4(202, 303);
fraction f5,f6;

cout<<"C++ CLASS MULTI-FILE PROJECT"<<endl;
cout<<"Client.cpp - testing a fraction class implementation\n";
cout<<"----------------------------------------------------\n\n";

cout << "The result object starts off at ";
result.showFraction(); //calling a void "observer" function
cout << endl;


cout<<"\nArithmetic operations with fraction objects stored in the results class object\n";
cout<<"------------------------------------------------------------------------------\n\n";

cout << "The sum of ";
f1.showFraction();
cout << " and ";
f2.showFraction();
cout << " is ";
result = f1.AddedTo(f2); //a class binary operation - a value-returning "observer" function
result.showFraction();
cout << endl;

cout << "The difference of ";
f1.showFraction();
cout << " and ";
f2.showFraction();
cout << " is ";
result = f1.Subtract(f2); //a class binary operation - a value-returning "observer" function
result.showFraction();
cout << endl;

cout << "The product of ";
f1.showFraction();
cout << " and ";
f2.showFraction();
cout << " is ";
result = f1.MultipliedBy(f2); //a class binary operation - a value-returning "observer" function
result.showFraction();
cout << endl;

result = f3.DividedBy(f4); //a class binary operation - a value-returning "observer" function
cout << "The quotient of ";
f3.showFraction();
cout << " and ";
f4.showFraction();
cout << " is ";
result.showFraction();
cout << endl;

cout<<"\nInput and Output operations for two new class objects\n";
cout<<"-----------------------------------------------------\n\n";

f5.getFraction(); //a class input operation - a transformer or setter function
f5.showFraction(); //a class output operation - an observer or getter function
cout<<endl<<endl;
f6.getFraction();
f6.showFraction();
cout<<endl<<endl;

cout<<"\nA Boolean operation comparing two class objects\n";
cout<<"------------------------------------\n\n";

if (f5.isGreaterThan(f6)){ //a class relational expression - boolean operation/function
f5.showFraction();
cout <<" is greater than ";
f6.showFraction();
cout<<endl;
} else {
f5.showFraction();
cout <<" is less than ";
f6.showFraction();
cout<<endl;
}

cout<<"\n---------------------------------------------------------\n";
cout<<"\nFraction class implementation test now successfully concluded\n";

// system ("PAUSE");

return 0;
}

But I have the same error.s
Why not just make everything in 1 file?

Otherwise tell us the specific command line commands you use for compiling or show the full build log in the IDE you are using, since this is a linker error. (but I have not tried to compile the code myself since it seems quite plain).

I think there are online compilers out there that have support for multiple files, like onlinegdb.com, try your code in that.

Since this is a trivial problem that should be learned quickly, you should stop bothering people with your problems, and look into a tutorial which is a step above cplusplus's minimal tutorial (since I don't believe it shows any examples of setting up your compiler or IDE), try lazyfoo's website (this steps into graphical programming, but I believe it has a lot to teach in a short context that anyone can understand), or get an entry level book which has compilable examples.

Also I see you are not guarding your headers, use the ifdef+define+endif combo you may have seen before, or use #pragma once at the top of the header file. I hope you only took it off while copy and pasting because you thought it didn't look important. But it is not the cause of your error.

Also next time use the <> button while editing your text to insert code formatting (I find it doesn't work while creating a topic in my browser, you gotta memorize the [code] code here [/code], and read the beginners sticky once, because shouldn't make a topic called "Help with Programming".

My code for client has:

#include <iostream>
#include "fraction.h"


thats better :)

Now, your actual problem that the compiler/linker is complaining about, is that it can't find "main" so we need to check that you told it to build it.

when you build your program all of the .cpp files (THAT YOU SPECIFY) get converted to some form of machine code and stored in .o/.obj files. If all the cpp files compile ok, the next step is to "link" all of the .o/.obj files into an exe

so in all of the files that the linker was told to link, it could not find "main" but we know it exists because its in the source code client.cpp so have to presume that the linker wasn't told about client.o/.obj, is there a client.o/.obj file in your folders?

so, what we need from you...

what IDE/compiler are you using to create/build your c++ project? Without that info we can't really say what you should check.

also post the copied output of your build so we can see the errors "in situ"
Last edited on
Topic archived. No new replies allowed.