Thread: trying to use forward declaration

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    52

    trying to use forward declaration

    I am trying to declare a vector pointer, and an ifstream pointer in a class that I plan to use to read binary files. I have heard from someone that using #include in header is often not best (not sure why), that it is better to use "class <name>", and then in the *.cpp file do the #include <name> call.

    I have tried doing that in my code but it doesn't work. I have tried this with some qt classes before and it works.

    Code:
    // #include <vector>
    // #include <fstream>
    class std::vector<uint32_t>;
    class std::fstream;
    I get this error:

    Code:
    g++ -c -Wall main.cpp -o main.o
    error: 'vector' is not a template
    error: 'uint32_t' was not declared in this scope
    error: 'vector' in namespace 'std' does not name a type
    error: 'fstream' in namespace 'std' does not name a type
    error: 'uint32_t' does not name a type
    error: using-declaration for non-member at class scope
    error: expected ';' before '*' token
    make: *** [main.o] Error 1

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The wisdom of not including headers aside (I'm a fan of including headers, especially the standard headers), it really doesn't work for templated things (without a lot more syntax). And even fstream is templated, as I recall. My advice is to include the headers.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Vector takes two template arguments at minimum, as well. And as I recall, the standard does not guarantee how many template parameters a vector takes.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM