Click to See Complete Forum and Search --> : newbie String question


hebes_99
August 24th, 2004, 09:54 AM
I am having a problem passing a string ref into a class function....

in Header file.....

#include <string>

class Process
{
public:
Process();
virtual ~Process();
int Process::GetBuno(const string& buno);
};



in Main file

#include "stdafx.h"
#include "Process.h"

int Process::GetBuno(const string& buno)
{

// some code

return buno;

}






When compiled... I get ..
(19) : error C2143: syntax error : missing ',' before '&'
(19) : error C2059: syntax error : '&'


Thoughts?

engineer2004
August 24th, 2004, 10:04 AM
Why are you returning a "string" object when your function return type is "int"?

Your function prototype should be.

string Process::GetBuno(const string& buno);

hebes_99
August 24th, 2004, 10:09 AM
Sorry... Meant to have say this..

int Process::GetBuno(const string& buno)
{

int number;
// some code

return number;

}

Bob Davis
August 24th, 2004, 10:10 AM
You also need to observe the fact that string is declared in the std namespace. Add the following to your .h and .cpp files:

using std::string;

engineer2004
August 24th, 2004, 10:11 AM
In your class header get rid of

Process::

it should just be

int GetBuno(const string& buno);

hebes_99
August 24th, 2004, 10:13 AM
Ok...that helped some... still getting the same errors...and

error C2061: syntax error : identifier 'string'

hebes_99
August 24th, 2004, 10:16 AM
Thanks Bob and Engineer..that got it!

NMTop40
August 24th, 2004, 11:59 AM
You also need to observe the fact that string is declared in the std namespace. Add the following to your .h and .cpp files:

using std::string;

Preferable not to do that in header files, but you might do it in .cpp files.

Andreas Masur
August 24th, 2004, 12:43 PM
All the classes of the STL are residing in their own namespace which is 'std'.

The following shows you the four different methods to map a namespace...

// Using the full member name, including the namespace it belongs to:
std::cout << "Hello World";


// By taking advantage of Using-Declarations:
using std::cout; // This declares cout in the current
// scope as synonym for std::cout
cout << "Hello World";


// By taking advantage of Using-Directives:
using namespace std; // Which specifies that the current
// scope can refer to names in the
// 'std' namespace without using
// full qualifiers. This is mostly
// used when porting legacy code.
cout << "Hello World";


// Using aliases:
namespace X
{
namespace Y
{
class Z { ... };
}
}

X::Y::Z // The full qualifier for 'Z' is
// 'X::Y::Z'

namespace w = X::Y; // This declares 'w' as an alias for
// namespace 'X::Y'

w::Z // Access 'Z' using 'w::Z'

Bob Davis
August 24th, 2004, 03:09 PM
Preferable not to do that in header files, but you might do it in .cpp files.

Good point. Inside the header file, it would be advisable to use the fully-qualified name for the class; i.e. std::string. Putting "using" declarations in header files might inadvertently bring classes into the global namespace when you include those headers in other source files.