std::string compile error

Started by
7 comments, last by Emmanuel Deloget 18 years ago
The source code below are the source codes of what is causing the problems.

Skin.cpp:228: 
std::cout << skinConfig.GetConfigValue( para_evaluate.substr( para_evaluate.find(".")+1, para_evaluate.length() - para_evaluate.find(".") - 1 ) );


Skin.cpp:286:
if ( skinConfig.GetConfigValue( para_evaluate.substr( para_evaluate.find("[")+1, para_evaluate.find("]") - para_evaluate.find("[") - 1 ) ).c_str() != "" )

Skin.cpp:287:
para_evaluate.replace( para_evaluate.find("["), para_evaluate.find("]") - para_evaluate.find("[") + 2, skinConfig.GetConfigValue( para_evaluate.substr( para_evaluate.find("[")+1, para_evaluate.find("]") - para_evaluate.find("[") - 1 ) ) );

Config.h:18: 
std::string GetConfigValue(std::string& key){
			return config[key];
		};



and these are the error messages.

Skin.cpp: In member function `void Skin::EvaluateFunction(std::string)':
Skin.cpp:228: error: no matching function for call to `Config::GetConfigValue(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
Config.h:18: note: candidates are: std::string Config::GetConfigValue(std::string&)
Skin.cpp:286: error: no matching function for call to `Config::GetConfigValue(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
Config.h:18: note: candidates are: std::string Config::GetConfigValue(std::string&)
Skin.cpp:287: error: no matching function for call to `Config::GetConfigValue(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
Config.h:18: note: candidates are: std::string Config::GetConfigValue(std::string&)


can you tell me what's wrong? because everything seems right to me. [Edited by - Tradone on March 30, 2006 7:59:19 PM]
Advertisement
Actually I know how to get rid of those deprecated warning messages, it's to make all of my
#include <fstream.h> into
#include <fstream>

but I think they're different files, and some methods don't seem to exist. But I'll double check....
My guess is that you forgot #include <string> somewhere (probably Config.h).
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Quote:Original post by Tradone
Actually I know how to get rid of those deprecated warning messages, it's to make all of my
#include <fstream.h> into
#include <fstream>

but I think they're different files, and some methods don't seem to exist. But I'll double check....


From the C++ FAQ Lite: What's the difference between <xxx> and <xxx.h> headers?
hmm...
You are comparing two pointers:
if (skinConfig.GetConfigValue(...).c_str() != "" )
Thats wrong! I think you need compare strings, right? Then:
if (skinConfig.GetConfigValue(...) != "" )
The problem is that the key argument to the GetConfigValue function should be a const reference:

std::string GetConfigValue(const std::string& key)

std::string::substr returns a temporary std::string object and that cannot be converted to a std::string&.
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
hm.. that's strange...
last time I changed all of my
#include <fstream.h>
into
#include <fstream>

I was getting compile errors, but it seems to be fine now. lool
Quote:Original post by dalleboy
The problem is that the key argument to the GetConfigValue function should be a const reference:

std::string GetConfigValue(const std::string& key)

std::string::substr returns a temporary std::string object and that cannot be converted to a std::string&.


right.
This is what I added in the Config.h header

std::string GetConfigValue(const std::string& key){
return config[key];
};

it compiled!! Thanks.
and I still don't seem to see the difference between,

std::string
std::string&
const std::string
const std::string&

and other forms of those.


To simplify:

1) void func(std::string v) { ... };
When you call func(), a new std::string object is created on the stack. It is destroyed when you leave the function. You can assign a value to v in func() but the value will not be propagated to the caller. This form should be avoided if the parameter type is not trivial or if its size is too big.

2) void func(std::string& v) { ... };
You supply a reference to a string, meaning that you don't copy the string - therefore it must exist as a std::string object before calling the function. The function can modify the string v, and the modification will impact the parameter.

3) void func(const std::string v) { ... };
Same as (1), except that you can't assign a value to v. Remember that in (1), you can assign a value to v - even if the value is not propagated to the caller. Don't use this.

4) void func(const std::string& v) { ... };
If you pass an existing std::string to the function, you only pass the reference. Since it is a const reference, it will not be modified in func(). If you feed func() with an entity that can be static_cast<>ed to std::string (meaning that std::string have a constructor that accept such object), then a constant std::string will be created on the stack. You wont be able to modify the resulting string.

HTH,

This topic is closed to new replies.

Advertisement