Thread: Weird errors, please help.

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

    Weird errors, please help.

    Hi, Can someone please tell me how to fix this code? I can't continue till it is figured out. Thank you.

    Severity and Description Path Resource Location Creation Time Id
    candidate is: bool CompTree::isInternal(CompTree::Position&) Driver2 compTree.h line 17 1208892430959 2012
    candidate is: bool CompTree::isExternal(CompTree::Position&) Driver2 compTree.h line 18 1208892430959 2014
    candidate is: void CompTree::swap(CompTree::Position&, CompTree::Position&) Driver2 compTree.h line 20 1208892430959 2017
    expected `;' before '<' token Driver2 compTree.h line 24 1208892430958 2002
    ISO C++ forbids declaration of `vector' with no type Driver2 compTree.h line 24 1208892430958 2001
    `v' undeclared (first use this function) Driver2 compTree.h line 29 1208892430958 2004
    invalid member function declaration Driver2 compTree.h line 30 1208892430958 2003
    `Position' does not name a type Driver2 compTree.cpp line 17 1208892430959 2005
    `Position' does not name a type Driver2 compTree.cpp line 21 1208892430959 2006
    `Position' does not name a type Driver2 compTree.cpp line 25 1208892430959 2007
    `Position' does not name a type Driver2 compTree.cpp line 29 1208892430959 2008
    `Position' does not name a type Driver2 compTree.cpp line 33 1208892430959 2009
    `Position' does not name a type Driver2 compTree.cpp line 37 1208892430959 2010
    prototype for `bool CompTree::isInternal(CompTree::Position)' does not match any in class `CompTree' Driver2 compTree.cpp line 41 1208892430959 2011
    prototype for `bool CompTree::isExternal(CompTree::Position)' does not match any in class `CompTree' Driver2 compTree.cpp line 45 1208892430959 2013
    `v' undeclared (first use this function) Driver2 compTree.cpp line 50 1208892430959 2015
    prototype for `void CompTree::swap(CompTree::Position, CompTree::Position)' does not match any in class `CompTree' Driver2 compTree.cpp line 56 1208892430959 2016
    make: *** [compTree.o] Error 1 Driver2 line 0 1208892430959 2018

    Code:
    #ifndef COMPTREE_H_
    #define COMPTREE_H_
    
    #include <vector>
    
    using namespace std;
    
    class CompTree {
    public:
    	class Position;
    	
    	CompTree();
    	Position add(int x);
    	Position last();
    	Position root();
    	Position leftChild(Position &p);
    	Position rightChild(Position &p);
    	Position sibling(Position &p);
    	Position parent(Position &p);
    	int remove();
    	int removeRoot();
    	bool isInternal(Position &p);
    	bool isExternal(Position &p);
    	bool isEmpty();
    	void swap(Position &p1, Position &p2);
    	int size();
    	~CompTree();
    	
    	vector<int> v;
    	
    	class Position {
    	public:
    		Position(int r) {rank = r;}
    		int element(){return v.at(rank);}
    		bool isNull{
    			if(rank == -1)
    				return true;
    			else
    				return false;
    		}
    		int getRank(){return rank;}
    		void setRank(int r){rank = r;}
    	private:	
    		int rank;
    	};
    		
    };
    #endif /*COMPTREE2_H_*/
    Code:
    #include "compTree.h"
    
    CompTree::CompTree(){}
    
    CompTree::Position CompTree::add(int x){
    	
    }
    
    int CompTree::remove(){
    	
    }
    
    int CompTree::removeRoot(){
    	
    }
    
    CompTree::Position CompTree::last(){
    	
    }
    
    Position CompTree::root(){
    	
    }
    
    Position CompTree::leftChild(Position &p){
    	
    }
    
    Position CompTree::rightChild(Position &p){
    	
    }
    
    Position CompTree::sibling(Position &p){
    	
    }
    
    Position CompTree::parent(Position &p){
    	
    }
    
    bool CompTree::isInternal(Position &p){
    	
    }
    
    bool CompTree::isExternal(Position &p){
    	
    }
    
    bool CompTree::empty(){
    	if(v.at(1) != -1)
    		return false;
    	else
    		return true;
    }
    
    void CompTree::swap(Position &p1, Position &p2){
    	
    }
    
    int CompTree::size(){
    	
    }
    
    CompTree::~CompTree(){};

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Where is Position defined?

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    inside the compTree class

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Oh yeah - didn't see it. Since CompTree member functions return Position by value, you need to define (not just forward declare) Position before CompTree.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You could start by fixing the Position class:
    Code:
    	class Position {
    	public:
    		Position(int r) {rank = r;}
    		int element(){return v.at(rank);} //Position does not have a member v
    		bool isNull{
    			if(rank == -1)
    				return true;
    			else
    				return false;
    		}
    		int getRank(){return rank;}
    		void setRank(int r){rank = r;}
    	private:	
    		int rank;
    	};
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    Quote Originally Posted by medievalelks View Post
    Oh yeah - didn't see it. Since CompTree member functions return Position by value, you need to define (not just forward declare) Position before CompTree.
    What do you mean declare it before CompTree? A couple functions that return a Position don't have errors only some do. Why is this?

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Put the whole Position class before CompTree.

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    I still get the same errors even with the Position class before CompTree.

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I wasn't really suggesting anything, I was explaining what medievalelks meant. To be honest, a prototype would have sufficed.

    Looking at the errors generated though, that does appear to be the problem. Just to make sure your compiler isn't on crack, try doing a make clean or something.

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    Im using elclipse with cygwin for the first time so I really don't know much about it. Could this combination cause a problem?

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The problem seems to be that the Position class has its own errors (as I pointed out). In addition, outside the declaration of the outer class, you should always refer to the inner class as outer::inner.

    Both MingW and Comeau Online were quite happy to compile it once these errors were fixed, even if you use a forward declaration of Position.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    What exactly is wrong with the position class? If I put the vector in the position class will I be able to access it with CompTree functions? Just to clarify I making a complete binary tree that holds ints and then making a heap with a heap sort function to sort a list of ints. I'm sorry for all these questions but this is for my data strutures class where the teacher is making us learn C++ for the first time as well as data structures so thats why I am very frustrated. Thanks for your time.

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    shouldn't be Position in the cpp file named CompTree::Position everywhere?

    Also it seems to me that
    Code:
     int element(){return v.at(rank);}
    should be member of CompTree, not Position
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by Rob4226 View Post
    What exactly is wrong with the position class? If I put the vector in the position class will I be able to access it with CompTree functions? Just to clarify I making a complete binary tree that holds ints and then making a heap with a heap sort function to sort a list of ints. I'm sorry for all these questions but this is for my data strutures class where the teacher is making us learn C++ for the first time as well as data structures so thats why I am very frustrated. Thanks for your time.
    The usual rules for accessing members of the outer class apply to nested classes too. You can't access a non-static member without an instance.

    You could do lots of things, such as passing a const reference of the vector to the methods of Position that need to know it. Or you could just call getRank and access the vector at that index outside Position
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Let's clarify here:
    A class is a blueprint. Let's say a car blueprint. You can't drive a car blueprint, can you? No. First, someone must make a car from a blueprint. This is called an instance of that class. An instance of a car from the car blueprint in this example.
    Then you can drive that car.
    The member "v" is not a part of Position; it's a part of another class, so you can't use it directly. Because the instance of Position will try to access "v" from a blueprint, which you can't do, can you? There must be an instance of that blueprint from where you can access "v," or alternatively, "v" could be moved inside Position in which case it would work since "v" is part of the Position blueprint, then "v" would exist inside the instance of the object created from the blueprint.

    Hope that makes sense.
    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. HELP with DX9 Errors!!!
    By Tommaso in forum Game Programming
    Replies: 7
    Last Post: 06-28-2006, 02:51 PM
  2. Errors with header files in OpenGL using VisualC++
    By wile_spice in forum Game Programming
    Replies: 3
    Last Post: 06-22-2006, 08:56 AM
  3. Weird Errors in VS 2003
    By Devil Panther in forum Windows Programming
    Replies: 1
    Last Post: 10-01-2005, 06:16 AM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 01:43 PM