1

I am working on an assignment for a class and I am running into (what I think is) a basic issueIve tried looking at other answers but I cannot seem to find my issue.

When I run the commmand make, I get the following error:

    prog8lib.o: In function `Transform':
   .../prog8lib.cc:6: multiple definition of `Transform::Transform(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
    prog8.o:.../prog8lib.cc:6: first defined here
    prog8lib.o: In function `Transform':
   .../prog8lib.cc:6: multiple definition of `Transform::Transform(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
    prog8.o:.../prog8lib.cc:6: first defined here

This is the code that I am currently using. Can anyone spot my errors?

Thanks!!!

prog8.cc:

#include <iostream>
#include <string.h>
#include <stdlib.h>

#include "prog8lib.cc"

using namespace std;

int main(int argc, char** argv) {
    string start = argv[1];
    string end = argv[2];
    Transform(start, end);
}

prog8lib.cc:

#include <iostream>
#include <string>
#include <stdlib.h>
#include "prog8lib.h"

Transform::Transform(string startWord, string endWord) {
    cout << "Test" << endl;
}
Transform::~Transform() {}

prog8lib.h:

#ifndef PROG8LIB_H
#define PROG8LIB_H

#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

class Transform{
private:
    string start;
    string end;
public:
    Transform(string str, string str2);
    ~Transform();
};

#endif

Makefile:

OBJ = prog8.o prog8lib.o
OPTS = -g -c -Wall -Werror

trans: $(OBJ)
        g++ -o trans $(OBJ)

prog8.o: prog8.cc
    g++ $(OPTS) prog8.cc

prog8lib.o: prog8lib.cc prog8lib.h
        g++ $(OPTS) prog8lib.cc

clean:
        rm -f *.o *~
wakey
  • 2,283
  • 4
  • 31
  • 58

2 Answers2

2

Change

#include "prog8lib.cc"

in prog8.cc to

#include "prog8lib.h"

Otherwise the compiler will find two definitions of Transform::Transform: once in prog8.cc and once in proglib8.cc.

cadaniluk
  • 15,027
  • 2
  • 39
  • 67
1

you have declared a class in header file, and that is the file that you need to include in main.cpp..always include the header files, not the .cpp files..

http://stackoverflow.com/questions/333889/why-have-header-files-and-cpp-files-in-c

basav
  • 1,475
  • 12
  • 20