Thread: including my extras.h file. errors

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    8

    Question including my extras.h file. errors

    Hi. I'm having some issues with compiling my program. I have several files: hw4-ffnn.cpp, network.{h,cpp}, neuron.{h,cpp}, and an extras.h file with a struct and some typedefs that I want common to all my files.
    Here's my extras.h file:
    Code:
    #ifndef _extras_h_
    #define _extras_h_
    
    #include <vector>
    #include <string>
    
    struct input_node{
    	bool training;
    	vector<int> pattern, target;
    };
    
    typedef vector<input_node> input;
    typedef vector< vector<int> > data;
    
    #endif
    I'm getting some inconsistent results when I use the g++ compiler with a few different options:
    Code:
    g++ hw4-ffnn.cpp -o hw4
    works fine (actually, i get some linking errors, but I don't think that they are relevant to this issue).
    However, when I do
    Code:
    g++ -c neuron.cpp network.cpp hw4-ffnn.cpp
    I get some errors:
    Code:
    extras.h:10: error: ISO C++ forbids declaration of 'vector' with no type
    extras.h:10: error: expected ';' before '<' token
    extras.h:13: error: expected initializer before '<' token
    extras.h:14: error: expected initializer before '<' token
    neuron.h:16: error: ISO C++ forbids declaration of 'data' with no type
    neuron.h:16: error: expected ';' before '*' token
    neuron.h:27: error: 'data' has not been declared
    neuron.cpp:15: error: variable or field 'set_weights' declared void
    neuron.cpp:15: error: 'int Neuron::set_weights' is not a static member of 'class Neuron'
    neuron.cpp:15: error: 'data' was not declared in this scope
    neuron.cpp:15: error: 'w' was not declared in this scope
    neuron.cpp:15: error: expected ',' or ';' before '{' token
    extras.h:10: error: ISO C++ forbids declaration of 'vector' with no type
    extras.h:10: error: expected ';' before '<' token
    extras.h:13: error: expected initializer before '<' token
    extras.h:14: error: expected initializer before '<' token
    neuron.h:16: error: ISO C++ forbids declaration of 'data' with no type
    neuron.h:16: error: expected ';' before '*' token
    neuron.h:27: error: 'data' has not been declared
    network.h:15: error: 'data' was not declared in this scope
    network.h:15: error: template argument 1 is invalid
    network.h:15: error: template argument 2 is invalid
    network.h:22: error: 'input' does not name a type
    network.h:27: error: expected `)' before '&' token
    network.cpp:4: error: expected `)' before '&' token
    network.cpp: In member function 'bool Network::category_exists(input_node&)':
    network.cpp:32: error: 'input_layer' was not declared in this scope
    network.cpp:33: error: 'struct input_node' has no member named 'target'
    make: *** [compile] Error 1
    Any ideas about what I'm missing?

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    use std::vector<type>.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    Quote Originally Posted by King Mir View Post
    use std::vector<type>.
    Actually, that didn't fix the issue, but I tried adding a
    Code:
    using namespace std;
    to the top and it worked. Also, that fixed the linking error that I mentioned.
    Thanks.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Never use using-directives or using-declarations in header files, since they contaminate any file that includes the header file. Use std:: instead as King Mir suggested. What may be happening is that you have the same problem in your .cpp files, and it's relatively okay to use using-directives or using-declarations in those, though only sparingly if at all.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    Quote Originally Posted by robatino View Post
    Never use using-directives or using-declarations in header files, since they contaminate any file that includes the header file. Use std:: instead as King Mir suggested. What may be happening is that you have the same problem in your .cpp files, and it's relatively okay to use using-directives or using-declarations in those, though only sparingly if at all.
    I appreciate the tip. Is this what you are talking about?
    Code:
    #ifndef _extras_h_
    #define _extras_h_
    
    #include <vector>
    #include <string>
    
    using std::vector;
    
    struct input_node{
    	bool training;
    	vector<int> pattern, target;
    };
    
    typedef vector<input_node> input;
    typedef vector< vector<int> > data;
    
    #endif

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By the way, names that begin with an underscore followed by a lower case letter are reserved to the implementation for use in the global namespace.

    Also, by convention, macro names are fully capitalised. As such, I suggest that you do not use header inclusion guard macro names of the form _extras_h_ but use say, EXTRAS_H_ instead.

    EDIT:
    Is this what you are talking about?
    No, more like:
    Code:
    #ifndef EXTRAS_H_
    #define EXTRAS_H_
    
    #include <vector>
    #include <string>
    
    struct input_node{
    	bool training;
    	std::vector<int> pattern, target;
    };
    
    typedef std::vector<input_node> input;
    typedef std::vector< std::vector<int> > data;
    
    #endif
    Last edited by laserlight; 02-17-2008 at 01:51 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Code:
    using namespace std;
    is a using-directive.
    Code:
    using std::vector;
    is a using-declaration. Don't use either one in header files, since either one contaminates any file that includes the header file. Use std::vector explicitly in your header file (since you only have to use it 4 times in your header file, it probably doesn't even take up more space than the one "using std::vector;" does anyway, and if you aren't even saving space, why bother?).

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by laserlight View Post
    By the way, names that begin with an underscore followed by a lower case letter are reserved to the implementation for use in the global namespace.
    Are you sure?
    Quote Originally Posted by Some C++ draft
    17.4.3.1.2 Global names [global.names]
    1 Certain sets of names and function signatures are always reserved to the implementation:
    — Each name that contains a double underscore _ _ or begins with an underscore followed by an uppercase letter
    (2.11) is reserved to the implementation for any use.
    — Each name that begins with an underscore is reserved to the implementation for use as a name in the global
    namespace.169)

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Look carefully at the second rule:
    Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace.

    As a consequence of the first rule, "names that begin with an underscore followed by a lower case letter are reserved to the implementation for use in the global namespace". Of course this is a more specific instance of "names that begin with an underscore followed by a character that is not an underscore or uppercase letter are reserved to the implementation for use in the global namespace" (as is the name that is the single underscore).
    Last edited by laserlight; 02-17-2008 at 11:24 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    should that really keep me from using that names for myself?

    Code:
    ::_funny_func();              // the global ns function
    myns::_funny_func();     // my own implementation
    That's that namespaces are for, so names don't collide?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    should that really keep me from using that names for myself?
    Frankly, it is easier to just avoid names that begin with an underscore unless you are writing a compiler or implementing the standard library.

    That's that namespaces are for, so names don't collide?
    Macro names are oblivious to namespaces.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Yes, I should really look these things over first. heh.

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    1
    Code:
    #ifndef _extras_h_
    #define _extras_h_
    
    #include <vector>
    #include <string>
    
    struct input_node{
    	bool training;
    	vector<int> pattern, target;
    };
    
    typedef vector<input_node> input;
    typedef vector< vector<int> > data;
    
    #endif
    Any ideas about what I'm missing?[/QUOTE]

    input_node is not a type within the vector. Try doing this:

    Code:
    typedef struct input_node{
    	bool training;
    	vector<int> pattern, target;
    }input_node;
    It should work. Good luck.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Replies: 9
    Last Post: 07-01-2002, 07:50 AM