Для школьного проекта класс попросили написать String
класс, имитирующий класс STL string
.
У меня есть весь написанный код, но, похоже, компоновщик захвачен одним из моих операторов.
Существует три файла: String.h
, String.cpp
и test2.cpp
Мой Makefile
выглядит как
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
выводит следующее:
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
Это странно, поскольку единственное место, которое я определяю operator ==
, находится в String.h
:
#ifndef MY_STRING_H
#define MY_STRING_H
#include <ostream>
#include <istream>
class String {
//...
};
// ... operators ...
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; }
// ... other comparison operators ...
#endif
test2.cpp
имеет только голый main
метод:
#include "String.h"
using namespace std;
int main() {
}
Итак, если я определяю operator ==(const String&, const char*)
только в одном месте, почему он говорит, что у меня есть дубликат символа?