Jump to content
  • Advertisement
Sign in to follow this  
Tradone

std::string compile error

This topic is 4558 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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....

Share this post


Link to post
Share on other sites
hmm...
You are comparing two pointers:
if (skinConfig.GetConfigValue(...).c_str() != "" )
Thats wrong! I think you need compare strings, right? Then:
if (skinConfig.GetConfigValue(...) != "" )

Share this post


Link to post
Share on other sites
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&.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.


Share this post


Link to post
Share on other sites
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,

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!