Compilation error with seperate compilation

Discussion in 'C++' started by C__chp, Feb 15, 2008.

  1. C__chp

    C__chp Guest

    Can anyone help me with the following problem
    When i compile a program which is devided in three files and i compile
    with GCC i'm getting the follwong error
    However when i put it in one file it conpiles and runs perfectly
    So it has probaly something to do with the includes , but i do not see
    what
    Many regards
    Nico Heiligers

    **** Build of configuration Debug for project acc ****
    **** Internal Builder is used for build ****
    g++ -O0 -g3 -Wall -c -fmessage-length=0 -oreadadfile.o ..
    \readadfile.cpp
    In file included from ..\readadfile.cpp:1:
    ...\adfile.h:12: error: ISO C++ forbids declaration of `vector' with no
    type
    ...\adfile.h:12: error: expected `;' before '<' token
    ...\adfile.h:14: error: `string' does not name a type
    ...\readadfile.cpp:5: error: expected initializer before "Adfile"
    ...\readadfile.cpp:6: error: expected constructor, destructor, or type
    conversion before '.' token
    ...\readadfile.cpp:7: error: expected constructor, destructor, or type
    conversion before '.' token
    ...\readadfile.cpp:8: error: expected declaration before '}' token
    Build error occurred, build is stopped
    Time consumed: 172 ms.

    these are the files
    h file for class decalration adfile.h
    #ifndef ADFILE_H_
    #define ADFILE_H_

    class Adfile
    {
    public:
    Adfile(char*);
    ~Adfile();
    void readFile(void);
    void displayFile(void) const;
    private:
    vector<string> v;
    char* filename;
    string line;
    };
    #endif /*ADFILE_H_*/

    the compiler is marking an error at lines

    vector<string> v;
    and
    string line

    ===============================

    implementation cpp file: adfile.cpp
    #include "adfile.h"
    #include <string>
    #include <iostream>
    #include <fstream>
    #include <vector>
    using namespace std;


    Adfile::Adfile(char* str)
    {
    filename = str;
    }
    Adfile::~Adfile()
    {
    }

    void Adfile::readFile(void)
    {
    ifstream in(filename);
    while(getline(in, line))
    v.push_back(line);
    }

    void Adfile::displayFile(void) const
    {
    for(unsigned int i = 0; i < v.size(); i++)
    cout << i << ": " << v << endl;
    }

    main program file:
    #include "adfile.h"

    int main(void)
    {
    Adfile adfile("Fillvector.cpp");
    adfile.readFile();
    adfile.displayFile();
    } ///:~

    ==========================================
    C__chp, Feb 15, 2008
    #1
    1. Advertising

  2. On Feb 15, 11:30 am, C__chp <> wrote:
    > the compiler is marking an error at lines
    >
    > vector<string> v;
    > and
    > string line
    >
    > ===============================
    >
    > implementation cpp file: adfile.cpp
    > #include "adfile.h"
    > #include <string>
    > #include <iostream>
    > #include <fstream>
    > #include <vector>
    > using namespace std;
    >


    You aren't including the headers for the stl vector and string in the
    class's header, so it doesn't understand what a vector<string> is
    Keith Halligan, Feb 15, 2008
    #2
    1. Advertising

  3. Keith Halligan wrote:
    > On Feb 15, 11:30 am, C__chp <> wrote:
    >> the compiler is marking an error at lines
    >>
    >> vector<string> v;
    >> and
    >> string line
    >>
    >> ===============================
    >>
    >> implementation cpp file: adfile.cpp
    >> #include "adfile.h"
    >> #include <string>
    >> #include <iostream>
    >> #include <fstream>
    >> #include <vector>
    >> using namespace std;
    >>

    >
    > You aren't including the headers for the stl vector and string in the
    > class's header, so it doesn't understand what a vector<string> is


    And the solution is .....

    move #include "adfile.h" 5 lines down, after #include <vector>

    Ko vd Sloot
    Ko van der Sloot, Feb 15, 2008
    #3
  4. In message <e85e2$47b58e93$89382996$>, Ko van der
    Sloot <> writes
    >Keith Halligan wrote:
    >> On Feb 15, 11:30 am, C__chp <> wrote:
    >>> the compiler is marking an error at lines
    >>>
    >>> vector<string> v;
    >>> and
    >>> string line
    >>>
    >>> ===============================
    >>>
    >>> implementation cpp file: adfile.cpp
    >>> #include "adfile.h"
    >>> #include <string>
    >>> #include <iostream>
    >>> #include <fstream>
    >>> #include <vector>
    >>> using namespace std;
    >>>

    >>
    >> You aren't including the headers for the stl vector and string in the
    >> class's header, so it doesn't understand what a vector<string> is

    >
    >And the solution is .....
    >
    >move #include "adfile.h" 5 lines down, after #include <vector>


    No, it's to put both "#include <vector>" and "#include <string>"
    *inside* adfile.h, and to prefix all occurrences of "vector" and
    "string" in that file with std:: .

    Since adfile.h refers to both "vector" and "string" it's only courtesy
    to the reader (and the compiler ;-) to explain what they are supposed to
    mean. Relying on the user to include headers in a particular order is
    not a robust solution.

    --
    Richard Herring
    Richard Herring, Feb 15, 2008
    #4
  5. C__chp

    Puppet_Sock Guest

    On Feb 15, 8:07 am, Ko van der Sloot <> wrote:
    [snips]
    > And the solution is .....
    >
    > move #include "adfile.h" 5 lines down, after #include <vector>


    Someplace I read that you want the first line of a class
    implementation file to be the #include of the header file
    for that class. This is a rule that has worked for me.

    So, as Richard Herring suggests, I'd put the #include for
    the STL stuff in the header file for a class that uses them.
    Socks
    Puppet_Sock, Feb 15, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kwaj
    Replies:
    2
    Views:
    450
    Grigorios Angelis
    Mar 4, 2004
  2. Mark
    Replies:
    10
    Views:
    737
    Joe Smith
    Jul 23, 2004
  3. anupam
    Replies:
    2
    Views:
    909
  4. Chris Mantoulidis
    Replies:
    3
    Views:
    406
    Jared Dykstra
    Dec 20, 2003
  5. moondaddy
    Replies:
    2
    Views:
    566
    Steven Cheng[MSFT]
    Oct 16, 2006
Loading...

Share This Page