Hello all,
I'm trying to link two files but I get error "multiple definition of `funny_words()' " I don't know what to do, as I'm new to C++. I'm reading PROGRAMMING IN C++ by P.B. Mahapatra and the topic is PREPROCESSOR. I have to learn how to Link headers, and cpp files using # include. My first trial with cpp have failed.

Soo much words, but here are two source codes:
1. main.cpp

#include <iostream>
#include "hello.cpp"
using namespace std;
//declare the class
class triangle{
    //only visiblein class, invisible outside
    private:
        int base;
        int height;
        int area;
    //visible both in and outta class
    public:
        int triangle_area();
        int get_values();
};

int triangle::triangle_area()
{
    area = 0.5*base*height;
    return(area);
}

int triangle::get_values()
{
    //prompt for triangle base
    cout<<"Please Enter the value of the triangle base"<<endl;
    cin>>base;
    //prompt for triangle height
    cout<<"Please Enter the value of the triangle height"<<endl;
    cin>>height;
}

int main()
{
    class triangle ABC;
    ABC.get_values();
    int area;
    area = ABC.triangle_area();
    cout<<"The area of Triangle is "<<area <<endl;
    funny_words();
    return 0;
}

2. hello.cpp

#include <iostream>
#include <cmath>
#define GEQUAL >=

using namespace std;
//function to display some funny words
int funny_words()
{
    int iq_test;
    cout<<"What is your IQ ?"<<endl;
    cin>>iq_test;
    if (iq_test GEQUAL 50)
        cout<<"You got it! Excellent!"<<endl;
    else
        cout<<"Sorry! you need to pray more for wisdom!"<<endl;
}

Thanks all!

Recommended Answers

All 2 Replies

in main.cpp, delete line 2 #include "hello.cpp". NEVER EVER include a *.c or *.cpp file in another one like that. If main.cpp needs to know about that function, then just use extern keyword, like this:

#include <iostream>
using namespace std;

extern int funny_words();
// blabla

Also, funny_words() is declared to return an integer. You must add code to do that on line 16. If you don't need it to return anything then change it to void function void funny_words()

commented: You should have mentioned the "#define GEQUAL" horror as well :) +29

That is great, man!
I removed # include "hello.cpp" and added return 0 at end of funny_words() and used extern int funny_words()

Guess what! It worked!
Thanks alot sir! Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.