What constructor or overloaded operator= am I missing?
From: William Payne (mikas_n_o_s_p_a_m_493_at_student.liu.se)
Date: 03/31/04
- Next message: John Harrison: "Re: Generating Compiler Warnings"
- Previous message: Xilo Musimene: "Re: Send/Receiving Home-made IP Packets (TCP,ICMP,etc)"
- Next in thread: Pete Vidler: "Re: What constructor or overloaded operator= am I missing?"
- Reply: Pete Vidler: "Re: What constructor or overloaded operator= am I missing?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 31 Mar 2004 18:30:31 +0200
Hello, using my simple string class called String, I noticed I cannot do:
String s = "hi";
without getting the following compilation error:
error: conversion from `const char[3]' to non-scalar type `String' requested
Here's the class declaration for String, what have I forgotten to implement
in order for the above line of code to compile:
class String
{
public:
String()
:
m_internal_string(NULL),
m_length(0)
{
; /* Do nothing. */
}
explicit String(const char* s);
explicit String(const String& s);
virtual ~String()
{
delete [] m_internal_string;
}
size_t length() const
{
return m_length;
}
const char* c_str() const
{
return m_internal_string;
}
void clear();
char operator[](std::size_t index) const;
String& operator+=(const char* s);
String& operator+=(const String& s);
String& operator=(const char* s);
String& operator=(const String& rhs);
bool operator==(const String& rhs);
friend std::ostream& operator<<(std::ostream& os, const String& s);
friend std::istream& operator>>(std::istream& is, String& s);
private:
char* m_internal_string;
std::size_t m_length;
};
Thanks for any replies
/ William Payne
- Next message: John Harrison: "Re: Generating Compiler Warnings"
- Previous message: Xilo Musimene: "Re: Send/Receiving Home-made IP Packets (TCP,ICMP,etc)"
- Next in thread: Pete Vidler: "Re: What constructor or overloaded operator= am I missing?"
- Reply: Pete Vidler: "Re: What constructor or overloaded operator= am I missing?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|