K
Karl
Hey everyone!
So I'm writing my own String class to wrap std::string and implement an
API as close to identical as possible to the Java API. It's pretty
small so far:
#include <string>
class String
{
public:
String(std::string s) : mString(s)
{
}
String(const char* s) : mString(s)
{
}
String()
{
}
std::string& toStdString()
{
return mString;
}
const std::string& toStdString() const
{
return mString;
}
const char* toCString() const
{
return mString.c_str();
}
bool operator==(const String& s) const
{
return mString == s.mString;
}
private:
std::string mString;
};
Trying to use '<<' with a String results in the following predictable
result with g++:
error: no match for 'operator<<' in 'oss << s'
So, my String class became the following:
#include <ostream>
#include <string>
class String
{
public:
String(std::string s) : mString(s)
{
}
String(const char* s) : mString(s)
{
}
String()
{
}
std::string& toStdString()
{
return mString;
}
const std::string& toStdString() const
{
return mString;
}
const char* toCString() const
{
return mString.c_str();
}
bool operator==(const String& s) const
{
return mString == s.mString;
}
private:
std::string mString;
public:
friend std:stream& operator<<(std:stream& os, const String& s);
};
std:stream& operator<<(std:stream& os, const String& s)
{
os << s.mString;
return os;
}
But now I get the following very unpredictable result with g++:
multiple definition of `operator<<(std::basic_ostream<char,
std::char_traits<char> >&, String const&)'
I get this on line 40, which is the line 'std:stream&
operator<<(std:stream& os, const String& s)', the first line of the
definition.
Anyone have any ideas? Thanks ahead of time!
So I'm writing my own String class to wrap std::string and implement an
API as close to identical as possible to the Java API. It's pretty
small so far:
#include <string>
class String
{
public:
String(std::string s) : mString(s)
{
}
String(const char* s) : mString(s)
{
}
String()
{
}
std::string& toStdString()
{
return mString;
}
const std::string& toStdString() const
{
return mString;
}
const char* toCString() const
{
return mString.c_str();
}
bool operator==(const String& s) const
{
return mString == s.mString;
}
private:
std::string mString;
};
Trying to use '<<' with a String results in the following predictable
result with g++:
error: no match for 'operator<<' in 'oss << s'
So, my String class became the following:
#include <ostream>
#include <string>
class String
{
public:
String(std::string s) : mString(s)
{
}
String(const char* s) : mString(s)
{
}
String()
{
}
std::string& toStdString()
{
return mString;
}
const std::string& toStdString() const
{
return mString;
}
const char* toCString() const
{
return mString.c_str();
}
bool operator==(const String& s) const
{
return mString == s.mString;
}
private:
std::string mString;
public:
friend std:stream& operator<<(std:stream& os, const String& s);
};
std:stream& operator<<(std:stream& os, const String& s)
{
os << s.mString;
return os;
}
But now I get the following very unpredictable result with g++:
multiple definition of `operator<<(std::basic_ostream<char,
std::char_traits<char> >&, String const&)'
I get this on line 40, which is the line 'std:stream&
operator<<(std:stream& os, const String& s)', the first line of the
definition.
Anyone have any ideas? Thanks ahead of time!