Thread: Strings in Header files

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    203

    Strings in Header files

    The following works fine:

    Header file:
    Code:
    #ifndef stringproj_h
    #define stringproj_h
    
    void show(int s);
    #endif
    Implementation file:
    Code:
    #include<iostream>
    #include "stringproj.h"
    
    using namespace std;
     
    void show (int s){
        cout<<s<<endl;
    }
    Client file
    Code:
    #include <iostream>
    #include"stringproj.h"
    
    using namespace std;
    
    int main(){
        show(7);
    }
    But when I try to extend the same framework to strings ...:
    Header file:
    Code:
    #ifndef stringproj_h
    #define stringproj_h
    
    #include<iostream>
    #include<string>
    void show(string s);
    #endif
    Implementation file
    Code:
    #include<iostream>
    #include<string>
    #include "stringproj.h"
    
    using namespace std;
     
    void show (string s){
        cout<<s<<endl;
    }
    Client file
    Code:
    #include <iostream>
    #include"stringproj.h"
    #include<string>
    
    
    using namespace std;
    
    
    int main(){
        show("Hello World");
    }
    ... get 2 error messages relating to implementation file:
    1. variable or field 'show' declared void;
    2. 'string' was not declared in this scope;

    and 1 error message relating to client file:
    1.'show' was not declared in this scope

    Suggestions on how to make strings work and any other observations would be much appreciated. Thanks
    Last edited by sean_cantab; 06-05-2016 at 05:55 AM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    void show(std::string s);
    string is declared in the std namespace. You get around that with using namespace std, but only in your implementation file. You don't do it in your header, so the compiler can't find it. Still, it is bad practice to use using namespace in headers, so explicitly add the std:: prefix to all standard library types in headers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    Many thanks for the quick reply. I see that using namespace std in the implementation file, at least in this example, does away with the need for using namespace std in the client file. In fact, again in this example, we can use just the scope resolution operator only in the implementation file though I can see why in larger chunks of code this might become messy:
    Code:
     std::cout<<s<<std::endl;
    Thanks once again.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    It may seem messy now but it will eventually become unnatural to not std:: if you start to always use std:: now instead of the "using" clauses. Also don't forget whitespace makes things easier to read!

    Code:
    std::cout << s << std::endl;
    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strings in header files
    By Aisthesis in forum C++ Programming
    Replies: 28
    Last Post: 08-03-2009, 06:30 AM
  2. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 12:21 PM
  3. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  4. Replies: 4
    Last Post: 12-14-2005, 02:21 PM
  5. include library header in header files
    By Raison in forum C++ Programming
    Replies: 6
    Last Post: 09-27-2004, 02:50 AM

Tags for this Thread