Problem with simple string function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TheMan1
    New Member
    • Mar 2008
    • 19

    Problem with simple string function

    Hi, I'm having a problem implementing a simple function Function1 that returns a string.

    Test1.h:
    Code:
    class Test1
    {
    	public:
    		Test1();
    		string Function1(string msg);
    };
    Test1.cpp:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    string Function1( string msg )
    {
    ...
    }
    I get errors such as "missing ';' before identifier 'Function1'", and "missing type specifier - int assumed", and "syntax error : identifier 'string'". These errors all occur in the header file.
    I'm very new to the C++ syntax, so I don't know how to fix it.
    Help? Thanks.
  • sanctus
    New Member
    • Mar 2007
    • 84

    #2
    I do not know the string library included so I do not know if string is a valid return type (I thought you can only use char)...but what seems to be missing in all cases is that you do not include the header in the test1.cpp file

    Originally posted by TheMan1
    Hi, I'm having a problem implementing a simple function Function1 that returns a string.

    Test1.h:
    Code:
    class Test1
    {
    	public:
    		Test1();
    		string Function1(string msg);
    };
    Test1.cpp:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    string Function1( string msg )
    {
    ...
    }
    I get errors such as "missing ';' before identifier 'Function1'", and "missing type specifier - int assumed", and "syntax error : identifier 'string'". These errors all occur in the header file.
    I'm very new to the C++ syntax, so I don't know how to fix it.
    Help? Thanks.

    Comment

    • TheMan1
      New Member
      • Mar 2008
      • 19

      #3
      Okay, I've now included Test1.h in the .cpp file. I still get the same errors outlined above.
      I'm not sure exactly what the string library I'm using is. I just don't know how to have the string type in C++, so was using that. But obviously that doesn't work at all, as it's giving me errors...
      Any suggestions? How would you get strings in C++?

      Comment

      • sanctus
        New Member
        • Mar 2007
        • 84

        #4
        Originally posted by TheMan1
        Okay, I've now included Test1.h in the .cpp file. I still get the same errors outlined above.
        I'm not sure exactly what the string library I'm using is. I just don't know how to have the string type in C++, so was using that. But obviously that doesn't work at all, as it's giving me errors...
        Any suggestions? How would you get strings in C++?

        I would try to use char arrays, but maybe this is stupid/non-efficient let's see what the experts say...

        Comment

        • Sick0Fant
          New Member
          • Feb 2008
          • 121

          #5
          I think that when you define the functions in the cpp file, you have to use scope resolution.

          i.e. test1::function 1(){//code here}

          Comment

          • Laharl
            Recognized Expert Contributor
            • Sep 2007
            • 849

            #6
            Did you #include <string> in your header file as well? You do need to if you didn't. (The preferred way to manage the potential for multiple inclusion here is to #include in the header file, then include the header file only in the implementation file since anything you include in the header will come with when you include that.) Also, in order to associate Function1 with your class, you need to use the scope resolution operator ::, so Function1 (in the .cpp only) becomes string Test1::Function 1(string msg).

            Comment

            • sanctus
              New Member
              • Mar 2007
              • 84

              #7
              Originally posted by Laharl
              Did you #include <string> in your header file as well? You do need to if you didn't. (The preferred way to manage the potential for multiple inclusion here is to #include in the header file, then include the header file only in the implementation file since anything you include in the header will come with when you include that.) Also, in order to associate Function1 with your class, you need to use the scope resolution operator ::, so Function1 (in the .cpp only) becomes string Test1::Function 1(string msg).

              I have used plenty of times (can't remember how many as most of you I guess) the function from a class and hence the scope resolution opearator...sho uld have seen it but I guess staying away a little while from coding can even make you forget the "obvious" stuff. Sorry, TheMan

              Comment

              Working...