For a school project, the class was asked to write a class Stringto simulate the STL class String.
I have all the code written, but it seems the linker is tied to one of my statements.
There are three files String.h, String.cppandtest2.cpp
Mine Makefilelooks like
CC=gcc
CXX=g++
CXXFLAGS+=-Wall -Wextra
LDLIBS+=-lstdc++
all:test2
test2:test2.o String.o
test2.o:test2.cpp String.h
String.o:String.cpp String.h
make outputs the following:
g++ -Wall -Wextra -c -o test2.o test2.cpp
g++ -Wall -Wextra -c -o String.o String.cpp
g++ test2.o String.o -lstdc++ -o test2
ld: duplicate symbol operator==(String const&, char const*)in String.o and test2.o
collect2: ld returned 1 exit status
make: *** [test2] Error 1
This is odd since the only place I define operator ==is in String.h:
#ifndef MY_STRING_H
#define MY_STRING_H
#include <ostream>
#include <istream>
class String {
};
bool operator ==(const String& left, const char* right)
{ return left.compare_to(right)==0; }
bool operator ==(const char* left, const String& right)
{ return right.compare_to(left)==0; }
bool operator ==(const String& left, const String& right)
{ return left.compare_to(right)==0; }
#endif
test2.cpponly has an open method main:
#include "String.h"
using namespace std;
int main() {
}
So, if I define only operator ==(const String&, const char*)in one place, why does he say that I have a duplicate?
source
share