Thread: Homework question

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    21

    Homework question

    g++ main.cpp user.cpp ad.cpp
    user.h:29: error: ISO C++ forbids declaration of 'vector' with no type
    user.h:29: error: expected ';' before '<' token

    Here is my user.h file:
    Code:
    #ifndef _user_h_
    #define _user_h_
    
    #include <string>
    #include <vector>
    #include "ad.h"
    using std::string;
    
    class user {
    
    public:
    
    	// CONSTRUCTORS
    	user(const string& _ID) : ID(_ID) {}
    
    	// ACCESSORS
    	const string& id() const { return ID; }
    
    	// MUTATORS
    	void add_keyword(const string& _keyword, const string& _URL, const float& _PC, const float& _CC, bool& _is_new);
    	bool ad_for_keyword(const string& _keyword, const string& _URL, const float& _PC) const;
    	bool click(const string& _keyword, const string& _URL) const;
    	bool remove(const string& _keyword) const;
    	void statistics(std::ostream & output_ofs);
    
    private:
    	// REPRESENTATION
    	string ID;
    	vector<ad> ads;
    };
    
    #endif
    I'd appreciate it if someone could help me with this. Commenting out the bolded line of code makes my programs compile perfectly.

    EDIT: Also, I'd appreciate if you didn't post any specific code to help me (stuff like the 2nd post is ok), not that I think this question needs it. My instructors allow me to seek help on comprehending error messages, but not get code from outside sources.
    Last edited by Jozrael; 02-07-2008 at 08:42 PM.

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    forbids declaration of 'vector' with no type. Is it an int, a char? That's the omission -

    examples: std::vector<char> or std::vector<int>

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    21
    But I want a vector of ads, a specific class I've declared in another .h and .cpp file. Thank you for your response, if I can give you any more information to help you, please tell me.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    A few points:
    * In general start class names with capitals.
    * put the word explicit before 1 argument constructors, to prevent automatic conversion.
    * don't put using std::string and similar statements in header files.

    And for the fix to your problem:
    Code:
    std::vector<ad> ads;
    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.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    21
    Thanks for your reply.

    1: I prefer to name classes with capitals, but they provided a main.cpp file for us to use, and it used constructors named 'user', so I went with it. Thanks for the tip though.

    2: explicit user(const string& _ID) : ID(_ID) {}

    ^ like that?

    3: I'm not sure why I shouldn't put 'using blah' in my header files: don't I need it to define for the compiler what it needs to have?

    And thank you for the fix...but I'm still not sure why it was necessary, since I defined #include <vector>. It works, so thank you, but I'm still not sure why.

    Thanks for your help mate

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    jozrael:
    these two things work the same but the second prevents I guess oyu would say scope problems
    Code:
    #include <vector>
    using std::vector;
    ...
    ...
    vector<int> blabla;
    or the prevent compiler confusion
    Code:
    #include <vector>
    ..
    ..
    std::vector<int> blabla;
    they both do the same thing but the 2nd is safer.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Jozrael View Post
    Code:
    2: explicit user (const string& _ID) : ID(_ID) {}
    ^ like that?
    Yep, that look right.

    3: I'm not sure why I shouldn't put 'using blah' in my header files: don't I need it to define for the compiler what it needs to have?
    Each header file serves as a module for linking functions and objects that it declares. As a modularization tool, it should provide the minimal impact on code that includes it -- it should only do what is necessary to use the attributed functions and objects. using statements go against this practice. There is no need for someone using your header to put string into the global namespace (which is what using statements do). It could lead to a name collision if the header file is included in a file that has a different global version of string.

    It's all in the spirit of modular, reusable code.

    I hope that's clear.

    And thank you for the fix...but I'm still not sure why it was necessary, since I defined #include <vector>. It works, so thank you, but I'm still not sure why.

    Thanks for your help mate
    Vector is defined in the std namespace. To use it you must either preface every use with std:: or before you use it have using std::vector or using namespace std. The same applies to std:: string. Since this is a header file putting std:: is the best solution.

    Read up on namespaces.
    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.

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    21
    Alright, I'm much clearer about why I shouldn't have 'using blah' in my header files, thank you for your help.

    I was confusing the #include and the using for the vector, thank you. I'm just starting to wean myself off the habit of 'using namespace std;' in all my files, so it'll take a bit. Its much clearer now. Thank you =).

    EDIT: Thank you raigne as well, I missed your comment, you also explained it. I was being silly, I get it now ^^. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question About My Homework Pls Help Me İmmediately
    By jennyyyy in forum C Programming
    Replies: 27
    Last Post: 03-13-2008, 11:40 AM
  2. quick question
    By surfingbum18 in forum C Programming
    Replies: 5
    Last Post: 12-04-2007, 06:16 AM
  3. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM