CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2010
    Posts
    21

    problem declaring a vector within a class

    I'm trying to declare two vectors within the private section of a class "rover" I am working on.




    Code:
    #ifndef ROVER_H
    #define ROVER_H              
    
    class rover
    {
    public:
    	// CONSTRUCTORS
    	rover();
    	//Default constructor
    	// POSTCONDITION: The rover is initialized with default coordinates (0,0) heading 
    	// North
    	
    	rover(double initial_x, double initial_y, char intitialHeading);
    	//Default constructor
    	// POSTCONDITION: The rover is initialized with coordinates heading specified
    	// by the user
    	
    	// MODIFICATION MEMBER FUNCTIONS
    	void move_forward(double meters);
    	//Function to move the rover forward a max of 25 meters
    	// POSTCONDITION: The rover has moved in the direction of its heading
    	// by the specified number of meters
    	
    	void move_backward(double meters);
    	//Function to move the rover in reverse a max of 4 meters
    	// POSTCONDITION: The rover has moved in the opposite direction of its heading
    	// by the specified number of meters
    
    	void turn_right();
    	//Function that turns the rover 90 degrees to the right
    	// POSTCONDITION: The rover's heading has moved 1 direction clockwise
    
    	void drill_core();
    	//Function that drills a core at current location and stores the location
    	//POSTCONDITION: A core has been drilled and its location has been stored
    
    
    	// CONSTANT MEMBER FUNCTIONS
    	void current_location() const;
    	//Function to report the rovers location and heading
    	// POSTCONDITION: The coordinates and heading are returned as an ordered
    	// triple
    
    	int core_quantity() const;
    	//Function that reports the number of cores drilled
    	//POSTCONDITION: the quantity of core samples has been outputed
    	
    	void core_report() const;
    	//Function that reports quantity of cores drilled as well as their locations
    	//POSTCONDITION:  All cores and their locations have been outputed
    	
    private:
    	double x;
    	double y;
    	char heading;
    	vector<double> core_x;
    	vector<double> core_y;
    	int quantity;
    	
    	//const double FORWARD_MAX = 25;
    	//const double REVERSE_MAX = -4;
    };
    
    #endif
    Whenever I try to run this with my implementation file I get the following errors:

    Error 1 error C2143: syntax error : missing ';' before '<'
    Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    Error 3 error C2238: unexpected token(s) preceding ';'
    Error 4 error C2143: syntax error : missing ';' before '<'
    Error 5 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    Error 6 error C2238: unexpected token(s) preceding ';'
    Error 7 error C2143: syntax error : missing ';' before '<'
    Error 8 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    Error 9 error C2238: unexpected token(s) preceding ';'
    Error 10 error C2143: syntax error : missing ';' before '<'
    Error 11 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    Error 12 error C2238: unexpected token(s) preceding ';'
    Error 13 error C2065: 'core_x' : undeclared identifier
    Error 14 error C2228: left of '.pushback' must have class/struct/union
    Error 15 error C2065: 'core_y' : undeclared identifier
    Error 16 error C2228: left of '.pushback' must have class/struct/union

    All of these seem to stem from the vectors. Can I not declare a vector there? Also, you can see where I commented out the two constants because they were giving me problems as well. I just got back into c++ after a summer break so I might have done something dumb.

  2. #2
    Join Date
    Feb 2010
    Posts
    21

    Re: problem declaring a vector within a class

    I've tried including #include <vector> at the top but it generates the same errors.

    When I add namespace std it fixes the previous errors but gives me the following error on my implementation file where I try to pushback:

    Error 1 error C2039: 'pushback' : is not a member of 'std::vector<_Ty>'

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Resolved Re: problem declaring a vector within a class

    Quote Originally Posted by jopeters View Post
    I've tried including #include <vector> at the top but it generates the same errors.
    You don't just "try" including, you must include <vector>.

    Once you do that, the vector class is in the std namespace.
    Code:
    std::vector<double> core_x;
    std::vector<double> core_y;
    When I add namespace std
    If you added "using namespace std" in the header, then that is not the correct approach. See above.
    but gives me the following error on my implementation file where I try to pushback:
    There is no such function as "pushback" in vector. The function name is "push_back".

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Feb 2010
    Posts
    21

    Re: problem declaring a vector within a class

    thank you Paul McKenzie, I made a silly mistake and forgot those underscores, but the program DOES run when I use "using namespace std;"

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: problem declaring a vector within a class

    Quote Originally Posted by jopeters View Post
    thank you Paul McKenzie, I made a silly mistake and forgot those underscores, but the program DOES run when I use "using namespace std;"
    It is not a matter of running or not running, it's a matter of program design.

    The reason why you should not put "using namespace std" in a header file is that anyone including your header must now have this namespace forced into their application without them doing anything.
    Code:
    #include "yourheader"
    
    struct vector
    {
        int x, y, z;
    };
    What if I had my own class called "vector"? Now I can't use it if I included your header because of the "using namespace std" (note that my vector is not in a namespace, and perfectly valid, but your use of "using namespace std" forces std on all of my custom code.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Feb 2010
    Posts
    21

    Re: problem declaring a vector within a class

    Thank you for your insite

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured