String in header file not working

Discussion in 'C++' started by Daviking, Feb 21, 2009.

  1. Daviking

    Daviking New Member

    Joined:
    Feb 21, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I am a beginning programmer, with almost no knowledge whatsoever. I am trying to make a very simple program with one header file, just to test out the principle. However, I cannot seem to get it to work. Whenever I try to compile, I get this error:

    in header file, 'string' does not have a type
    invalid conversion from 'const char*' to 'int'

    Here is my source code:

    //MULTIFILE.CPP (source file)
    #include <iostream>
    #include <string>
    #include "Multifile.h"
    using namespace std;
    int main()
    {
    cout << "Tripling 9: " << times3(9) << endl;
    cout << "Tripling 'game': " << times3("hello") << "\n\n";
    return 0;
    }

    //MULTIFILE.H (header file)
    #ifndef MULTIFILE_H
    #define MULTIFILE_H
    int times3(int number) { return (number * 3); }
    string times3(string text) { return (text + text + text); }
    #endif

    I have tried different "using" and std::string things in both the source and header, tried #including string in the header, and nothing seems to work... I keep getting the error. It does not seem to have anything to do with function overloading, and I am stuck.

    Please help!
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Move the "using" above the #include "Multifile.h" line.

    It's not a good idea to put function bodies in a header file because if that header file is included in more than one source file (which is the point of header files, so if you only have one source file you don't need one) then you're going to get duplicate symbols. So just get it all working in one file before splitting it out into headers.

    This all works fine in Visual Studio 2005 (main() just calls FUNC() which stops me needing millions of projects for go4e code testing):
    Code:
    #include <iostream>
    
    #define FUNC go4e_16291
    
    #include <string>
    using namespace std;
    
    int times3(int number) { return (number * 3); }
    string times3(string text) { return (text + text + text); }
    
    void go4e_16291()
    {
    cout << "Tripling 9: " << times3(9) << endl;
    cout << "Tripling 'game': " << times3("hello") << "\n\n";
    }
    
    Output:
    Tripling 9: 27
    Tripling 'game': hellohellohello
     
  3. Daviking

    Daviking New Member

    Joined:
    Feb 21, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Thank you. This works now.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice