Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 588093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:16:56+00:00 2026-05-13T15:16:56+00:00

For a school project, the class was asked to write a String class to

  • 0

For a school project, the class was asked to write a String class to mimic the STL string class.

I have all the code written, but the linker seems to be caught up on one of my operators.

There are three files, String.h, String.cpp, and test2.cpp

My Makefile looks 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 {
    //...
};

// ... 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 only has a bare main method:

#include "String.h"
using namespace std;

int main() {

}

So, if I only define operator ==(const String&, const char*) in one place, why does it say I have a duplicate symbol?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:16:56+00:00Added an answer on May 13, 2026 at 3:16 pm

    You provided the definitions of the operators in the header file which gets included by both String.cpp and test2.cpp.
    You should move the definitions into one source file and only provide declarations in the header file.

    // in String.h:
    bool operator==(const String& left, const char* right);
    
    // in String.cpp:
    bool operator ==(const String& left, const char* right) {
        return left.compare_to(right)==0; 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 288k
  • Answers 288k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I've been using the same code as you mention in… May 13, 2026 at 5:24 pm
  • Editorial Team
    Editorial Team added an answer Ok, solved this problem... My mapping file now looks like:… May 13, 2026 at 5:24 pm
  • Editorial Team
    Editorial Team added an answer If you don't need a back button and since part… May 13, 2026 at 5:24 pm

Related Questions

I ran into an interesting (and very frustrating) issue with the equals() method today
Hey all. I am working on a project for school where we are given
I recently took a class at school where we had to learn Scheme to
I'm using two different libraries in my project, and both of them supply a
My question is broader than merely expanding language specific skills. I'm wondering what sort

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.