0

I am trying to figure out how to make a program consisting of separate files. I read this post:

Function Implementation in Separate File

But have not succeeded. I have 3 files: main.cpp, func.cpp, time.h and when I compile, I get this error message:

duplicate symbol getOpen(std::basic_ofstream<char, std::char_traits<char> >&)in:
    /var/folders/kp/57zkm0tn1q7b7w0cs7tlf98c0000gn/T//cczaW1Px.o
    /var/folders/kp/57zkm0tn1q7b7w0cs7tlf98c0000gn/T//ccvOCRgc.o
ld: 1 duplicate symbol for architecture x86_64
collect2: ld returned 1 exit status

I have no idea what this means. I'm basically just trying to open a file, write to it, and close it. I also just created an object and tested it. I know the problem is from func.cpp because when I remove that it works. Can someone please advise? Thank you.

I type this to compile: g++ main.cpp func.cpp.

This is my code:

time.h

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
using namespace std;

class Time
{
    private:
        int seconds;
        int minutes;
        int hours;
    public:
        Time(int=0, int=0, int=0);
        Time(long);
        void showTime();
};

Time::Time(int sec, int min, int hour)
{
    seconds = sec;
    minutes = min;
    hours = hour;
}

Time::Time(long sec)
{
    hours = int(sec / 3600);
    minutes = int((sec % 3600)/60);
    seconds = int( (sec%60) );
}

void Time::showTime()
{
    cout << setfill('0')
         << setw(2) << hours << ':'
         << setw(2) << minutes << ':'
         << setw(2) << seconds << endl;
}

func.cpp

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

int getOpen(ofstream& fileOut)
{
    string filename = "outfile.txt";
    fileOut.open(filename.c_str());

    if( fileOut.fail())
    {
        cout << "\nFailed to open file.\n";
        exit(1);
    }
    else
        return 0;
}

main.cpp

#include <iostream>
#include "time.h"
#include "func.cpp"

int main()
{
    ofstream outFile;
    Time t1;

    t1.showTime();

    getOpen(outFile);

    outFile << "This is a test" << endl;

    outFile.close();

    return 0;
}
2
  • Also, is typical to include all function prototypes in a header file? Commented Nov 26, 2013 at 21:39
  • 2
    Yes, it is. In your case, it could be 'func.h'. And consider using #ifdef guards in the header files (to prevent including their contents twice). Commented Nov 26, 2013 at 21:40

2 Answers 2

1

You included "func.cpp" into main.cpp, so you have a double declaration of getOpen.

Sign up to request clarification or add additional context in comments.

1 Comment

It's the standard thing to do, indeed - prototypes in .h file, definitions in .cpp file (and include the header in the .cpp).
1

You shouldn't #include "func.cpp" into main.cpp.

Well, it depends on how you compile your program. If you compile only main.cpp, then you should include func.cpp. But that's not a nice way to write a program, and I hope you are not going to do that.

What you might want to do is to compile main.cpp and func.cpp separately (using gcc -c) and then link the .o files. That would be perfectly ok if you don't include one .cpp file into another. But when you include func.cpp into main.cpp, both of the .o files define getOpen function. That causes the error.

So: just remove the #include in main.

1 Comment

Thanks so much! could you give me the commands you would type to compile and link? I was just doing g++ main.cpp func.cpp?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.