|
-
August 24th, 2004, 09:54 AM
#1
newbie String question
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?
-
August 24th, 2004, 10:04 AM
#2
Re: newbie String question
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);
-
August 24th, 2004, 10:09 AM
#3
Re: newbie String question
Sorry... Meant to have say this..
int Process::GetBuno(const string& buno)
{
int number;
// some code
return number;
}
-
August 24th, 2004, 10:10 AM
#4
Re: newbie String question
You also need to observe the fact that string is declared in the std namespace. Add the following to your .h and .cpp files:
-
August 24th, 2004, 10:11 AM
#5
Re: newbie String question
In your class header get rid of
Process::
it should just be
int GetBuno(const string& buno);
-
August 24th, 2004, 10:13 AM
#6
Re: newbie String question
Ok...that helped some... still getting the same errors...and
error C2061: syntax error : identifier 'string'
-
August 24th, 2004, 10:16 AM
#7
Re: newbie String question
Thanks Bob and Engineer..that got it!
-
August 24th, 2004, 11:59 AM
#8
Re: newbie String question
 Originally Posted by Bob Davis
You also need to observe the fact that string is declared in the std namespace. Add the following to your .h and .cpp files:
Preferable not to do that in header files, but you might do it in .cpp files.
-
August 24th, 2004, 12:43 PM
#9
Re: newbie String question
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...
Code:
// 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'
-
August 24th, 2004, 03:09 PM
#10
Re: newbie String question
 Originally Posted by NMTop40
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|