473,406 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

compilation error: "error: no matching function for call to 'String::String(String)'

Hello again,

Sorry to bother but I guess my C++ book isn't very good since it
obviously contains errors so the program code doesn't work with g++.

However I don't understand what the problem is. Last time the problem
was that "using namespace std" apparently had a "link" class/object
defined already. Now I get:

error: no matching function for call to 'String::String(String)'

See if you can spot the error because I can't:

- - - - - - - - - - - - - - -
// memory-saving String class
// overloaded assignment and copy constructor
#include <iostream>
#include <cstring> //for strcpy(), etc.
using namespace std;
////////////////////////////////////////////////////////////////
class strCount //keep track of number
{ //of unique strings
private:
int count; //number of instances
char* str; //pointer to string
friend class String; //make ourselves available
//member functions are private
//--------------------------------------------------------------
strCount(char* s) //one-arg constructor
{
int length = strlen(s); //length of string argument
str = new char[length+1]; //get memory for string
strcpy(str, s); //copy argument to it
count=1; //start count at 1
}
//--------------------------------------------------------------
~strCount() //destructor
{ delete[] str; } //delete the string
};
////////////////////////////////////////////////////////////////
class String //String class
{
private:
strCount* psc; //pointer to strCount
public:
String() //no-arg constructor
{ psc = new strCount("NULL"); }
//--------------------------------------------------------------
String(char* s) //1-arg constructor
{ psc = new strCount(s); }
//--------------------------------------------------------------
String(String& S) //copy constructor
{
psc = S.psc;
(psc->count)++;
}
//--------------------------------------------------------------
~String() //destructor
{
if(psc->count==1) //if we are its last user,
delete psc; // delete our strCount
else // otherwise,
(psc->count)--; // decrement its count
}
//--------------------------------------------------------------
void display() //display the String
{
cout << psc->str; //print string
cout << " (addr=" << psc << ")"; //print address
}
//--------------------------------------------------------------
void operator = (String& S) //assign the string
{
if(psc->count==1) //if we are its last user,
delete psc; // delete our strCount
else // otherwise,
(psc->count)--; // decrement its count
psc = S.psc; //use argument's strCount
(psc->count)++; //increment its count
}
};
////////////////////////////////////////////////////////////////
int main()
{
String s3 = "When the fox preaches, look to your geese.";
cout << "\ns3="; s3.display(); //display s3

String s1; //define String
s1 = s3; //assign it another String
cout << "\ns1="; s1.display(); //display it

String s2(s3); //initialize with String
cout << "\ns2="; s2.display(); //display it
cout << endl;
return 0;
}

- - - - - - - - - - - - - - -

I didn't make the comments. I just copy/pasted the code so I don't want
to spend time on removing them as I also find it okay that they're there.
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
May 6 '06 #1
5 7179
"Martin Jørgensen" <un*********@spam.jay.net> wrote in message
news:4j************@news.tdc.dk
Hello again,

Sorry to bother but I guess my C++ book isn't very good since it
obviously contains errors so the program code doesn't work with g++.

However I don't understand what the problem is. Last time the problem
was that "using namespace std" apparently had a "link" class/object
defined already. Now I get:

error: no matching function for call to 'String::String(String)'

See if you can spot the error because I can't:
Change the String copy contructor to take a const reference rather than a
plain reference (copy constructors should always take const references).
--
John Carson

- - - - - - - - - - - - - - -
// memory-saving String class
// overloaded assignment and copy constructor
#include <iostream>
#include <cstring> //for strcpy(), etc.
using namespace std;
////////////////////////////////////////////////////////////////
class strCount //keep track of number
{ //of unique strings
private:
int count; //number of instances
char* str; //pointer to string
friend class String; //make ourselves available
//member functions are private
//--------------------------------------------------------------
strCount(char* s) //one-arg constructor
{
int length = strlen(s); //length of string argument
str = new char[length+1]; //get memory for string
strcpy(str, s); //copy argument to it
count=1; //start count at 1
}
//--------------------------------------------------------------
~strCount() //destructor
{ delete[] str; } //delete the string
};
////////////////////////////////////////////////////////////////
class String //String class
{
private:
strCount* psc; //pointer to strCount
public:
String() //no-arg constructor
{ psc = new strCount("NULL"); }
//--------------------------------------------------------------
String(char* s) //1-arg constructor
{ psc = new strCount(s); }
//--------------------------------------------------------------
String(String& S) //copy constructor
{
psc = S.psc;
(psc->count)++;
}
//--------------------------------------------------------------
~String() //destructor
{
if(psc->count==1) //if we are its last user,
delete psc; // delete our strCount
else // otherwise,
(psc->count)--; // decrement its count
}
//--------------------------------------------------------------
void display() //display the String
{
cout << psc->str; //print string
cout << " (addr=" << psc << ")"; //print address
}
//--------------------------------------------------------------
void operator = (String& S) //assign the string
{
if(psc->count==1) //if we are its last user,
delete psc; // delete our strCount
else // otherwise,
(psc->count)--; // decrement its count
psc = S.psc; //use argument's strCount
(psc->count)++; //increment its count
}
};
////////////////////////////////////////////////////////////////
int main()
{
String s3 = "When the fox preaches, look to your geese.";
cout << "\ns3="; s3.display(); //display s3

String s1; //define String
s1 = s3; //assign it another String
cout << "\ns1="; s1.display(); //display it

String s2(s3); //initialize with String
cout << "\ns2="; s2.display(); //display it
cout << endl;
return 0;
}

- - - - - - - - - - - - - - -

I didn't make the comments. I just copy/pasted the code so I don't
want to spend time on removing them as I also find it okay that
they're there.

Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk


May 6 '06 #2
Martin Jørgensen wrote:
Hello again,

Sorry to bother but I guess my C++ book isn't very good since it
obviously contains errors so the program code doesn't work with g++.

However I don't understand what the problem is. Last time the problem
was that "using namespace std" apparently had a "link" class/object
defined already. Now I get:

error: no matching function for call to 'String::String(String)'

See if you can spot the error because I can't:
Well, it would have be much easier to spot if you had included the line
number or better even marked the line that produced the error.

- - - - - - - - - - - - - - -
// memory-saving String class
// overloaded assignment and copy constructor
#include <iostream>
#include <cstring> //for strcpy(), etc.
using namespace std;
////////////////////////////////////////////////////////////////
class strCount //keep track of number
{ //of unique strings
private:
int count; //number of instances
char* str; //pointer to string
friend class String; //make ourselves available
//member functions are private
//--------------------------------------------------------------
strCount(char* s) //one-arg constructor
{
int length = strlen(s); //length of string argument
str = new char[length+1]; //get memory for string
strcpy(str, s); //copy argument to it
count=1; //start count at 1
}
//--------------------------------------------------------------
~strCount() //destructor
{ delete[] str; } //delete the string
};
////////////////////////////////////////////////////////////////
class String //String class
{
private:
strCount* psc; //pointer to strCount
public:
String() //no-arg constructor
{ psc = new strCount("NULL"); }
//--------------------------------------------------------------
String(char* s) //1-arg constructor
{ psc = new strCount(s); }
//--------------------------------------------------------------
String(String& S) //copy constructor
Here is probably the problem. Your copy constructor claims to modify the
original string, because the parameter is a non-const reference. Try:

String(const String& S) //copy constructor

Since your whole program doesn't care about const-correctness, I guess
that's the point where your book is failing.
{
psc = S.psc;
(psc->count)++;
}
//--------------------------------------------------------------
~String() //destructor
{
if(psc->count==1) //if we are its last user,
delete psc; // delete our strCount
else // otherwise,
(psc->count)--; // decrement its count
}
//--------------------------------------------------------------
void display() //display the String
{
cout << psc->str; //print string
cout << " (addr=" << psc << ")"; //print address
}
//--------------------------------------------------------------
void operator = (String& S) //assign the string
{
if(psc->count==1) //if we are its last user,
delete psc; // delete our strCount
else // otherwise,
(psc->count)--; // decrement its count
psc = S.psc; //use argument's strCount
(psc->count)++; //increment its count
}
};
////////////////////////////////////////////////////////////////
int main()
{
String s3 = "When the fox preaches, look to your geese.";
cout << "\ns3="; s3.display(); //display s3

String s1; //define String
s1 = s3; //assign it another String
cout << "\ns1="; s1.display(); //display it

String s2(s3); //initialize with String
cout << "\ns2="; s2.display(); //display it
cout << endl;
return 0;
}

- - - - - - - - - - - - - - -

I didn't make the comments. I just copy/pasted the code so I don't want
to spend time on removing them as I also find it okay that they're there.
Best regards / Med venlig hilsen
Martin Jørgensen


May 6 '06 #3
Martin Jørgensen wrote:
I guess my C++ book isn't very good since it obviously contains errors so
the program code doesn't work with g++.
Get /Accelerated C++/ by Koenig and Moo, so you can skip over these
low-level issues and learn to write useful programs sooner. And read more
than 3 tutorials.
However I don't understand what the problem is. Last time the problem was
that "using namespace std" apparently had a "link" class/object defined
already. Now I get:
And that's a reason not to use 'using namespace std'.
error: no matching function for call to 'String::String(String)'
On what line? Next time comment such a line with // <-- error
String(String& S) //copy constructor
Make that String(String const & S). Without the 'const', the compiler
automatically generates another copy constructor for you, with the signature
String(String const & S). Then on this line...
String s2(s3); //initialize with String


....the compiler can't disambiguate the two calls, or something.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
May 6 '06 #4
Phlip wrote:
String(String& S) //copy constructor


Make that String(String const & S). Without the 'const', the compiler
automatically generates another copy constructor for you, with the
signature String(String const & S). Then on this line...
String s2(s3); //initialize with String


...the compiler can't disambiguate the two calls, or something.


Reviewing the other two posts, I now realize I was the closest to the
answer. Strike "or something".

When a compiler can't disambiguate two calls, it declares that it can't find
the simplest thing that it was looking for. For example:

void foo(short x);
void foo(long x);
foo(10); // <-- will report a missing foo(int)

So the simplest thing it can infer from String s2(s3) is
String::String(String). So the error message asks for that constructor,
instead of the canonical String::String(String const &).

(Despite String::String(String) is an impossible constructor, which is an
unrelated point. If you wrote a copy constructor without any &, then s3
would call that copy constructor to copy into the parameter argument. And
that would recursively call the same copy constructor forever.)

I win!

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
May 6 '06 #5
John Carson wrote:
"Martin Jørgensen" <un*********@spam.jay.net> wrote in message
news:4j************@news.tdc.dk
Hello again,

Sorry to bother but I guess my C++ book isn't very good since it
obviously contains errors so the program code doesn't work with g++.

However I don't understand what the problem is. Last time the problem
was that "using namespace std" apparently had a "link" class/object
defined already. Now I get:

error: no matching function for call to 'String::String(String)'

See if you can spot the error because I can't:

Change the String copy contructor to take a const reference rather than a
plain reference (copy constructors should always take const references).


Thanks! Problem solved.
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
May 6 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Plat | last post by:
I've Googled this for a while, to no avail. Hopefully someone can help me. Maybe I'm using the wrong terminology. Here's the scoop! Let's say I've got a simple *.ASPX page that has a syntax...
4
by: john isaac | last post by:
this statement causes a compilation error: memberlist.name.push_back(newname); cannot convert std::string to char. memberlist is a vector...i is an integer that's interating in a...
2
by: JNariss | last post by:
I keep getting the following error message when trying to preview my page: Microsoft VBScript compilation error '800a0400' Expected statement /TESTESC.asp, line 33
1
by: Omatase | last post by:
Here is my code: CDO.Message iMessage = new CDO.MessageClass(); string sFrom; string sDate; iMessage.DataSource.Open(bstrURLItem,null, ADODB.ConnectModeEnum.adModeRead,...
2
by: subramanian | last post by:
Consider the following program: #include <iostream> #include <string> class Member { int x; int y; public: Member(int argx, int argy);
5
by: dancer | last post by:
Using Asp.net 1.1 Can somebody tell me why the code following gives this error message: Compilation Error Description: An error occurred during the compilation of a resource required to service...
9
by: subramanian100in | last post by:
Consider the following program: #include <iostream> #include <string> #include <vector> using namespace std; template<class Tclass Vec : public vector<T> {
2
by: subramanian100in | last post by:
Consider the following program: #include <iostream> #include <map> #include <string> #include <utility> #include <algorithm> using namespace std;
3
by: Indy | last post by:
Hi, I am new to VB and have some previous programming experiences. Curently working as an IT support person and trying to write a VB 6 script to access apos database and get one of the table's...
5
by: atulpatil09 | last post by:
when I am trying to compile the below code I am getting error as below, Could you please let me know how to solve the above compilation error for the below program snippet MList<MRvDispatcher...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.