CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Aug 2004
    Posts
    15

    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?

  2. #2
    Join Date
    May 2004
    Posts
    123

    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);

  3. #3
    Join Date
    Aug 2004
    Posts
    15

    Re: newbie String question

    Sorry... Meant to have say this..

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

    int number;
    // some code

    return number;

    }

  4. #4
    Join Date
    Jan 2001
    Posts
    588

    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:

    Code:
    using std::string;

  5. #5
    Join Date
    May 2004
    Posts
    123

    Re: newbie String question

    In your class header get rid of

    Process::

    it should just be

    int GetBuno(const string& buno);

  6. #6
    Join Date
    Aug 2004
    Posts
    15

    Re: newbie String question

    Ok...that helped some... still getting the same errors...and

    error C2061: syntax error : identifier 'string'

  7. #7
    Join Date
    Aug 2004
    Posts
    15

    Re: newbie String question

    Thanks Bob and Engineer..that got it!

  8. #8
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: newbie String question

    Quote 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:

    Code:
    using std::string;
    Preferable not to do that in header files, but you might do it in .cpp files.

  9. #9
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    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'

  10. #10
    Join Date
    Jan 2001
    Posts
    588

    Re: newbie String question

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured