Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C++: returning a string from a function.
New Posts  All Forums:Forum Nav:

C++: returning a string from a function.

post #1 of 3
Thread Starter 
Visual Studio decided today is the time to throw up cryptic errors.
Quote:
Error 1 error LNK2019: unresolved external symbol "class std::basic_string,class std::allocator > __cdecl open(void)" (?open@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function "int __cdecl input(void)" (?input@@YAHXZ) c:\Users\3930k\documents\visual studio 2012\Projects\topsecretapp\maindir\defp.obj topsecretapp

I'm guessing this has got to be with my usage of strings as a return type. However I dunno what I did wrong there because I used using namespace std; and also used using std::string, which is what I need iirc.
So I'm not sure what's wrong there.

The only problem is I'm not sure if it's worth the hassle to try and do string return types or I should just take a string reference instead.

EDIT: I've gotten it working using string&, but I'm leaving this on to ask which is better to use.
Edited by 3930K - 11/10/12 at 12:17pm
post #2 of 3
This is difficult to answer without seeing the code. Some reasons you may see an error like this include: missing some header inclusions (e.g. #include (should be string in the < > brackets here but it disappears)), declaring but not implementing a function, wrong configuration of the project linker settings (not likely since standard library should be added automatically).

As for using references vs returning a value, again it depends on what you're doing. Generally using a reference is more efficient because the function doesn't have to create a copy of the object, however it's not always possible, depending on the program structure. If it works for you then there's no reason not to use it.
Edited by poroboszcz - 11/10/12 at 1:26pm
buka
(17 items)
 
  
Reply
buka
(17 items)
 
  
Reply
post #3 of 3
Thread Starter 
Quote:
Originally Posted by poroboszcz View Post

This is difficult to answer without seeing the code. Some reasons you may see an error like this include: missing some header inclusions (e.g. #include (should be string in the < > brackets here but it disappears)), declaring but not implementing a function, wrong configuration of the project linker settings (not likely since standard library should be added automatically).

As for using references vs returning a value, again it depends on what you're doing. Generally using a reference is more efficient because the function doesn't have to create a copy of the object, however it's not always possible, depending on the program structure. If it works for you then there's no reason not to use it.
All of those I didn't do.

I'm passing a reference to a string to read a file and get it all into the string (getline(DBRead, rStr))

And yes,. DBRead is my reader and rStr is my reference.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C++: returning a string from a function.