Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

is not a class or namespace name compiler error 1

Status
Not open for further replies.

tmcneil

Technical User
Nov 17, 2000
294
US
I have three of the same compiler errors for one class that I have developed and I am not sure why this occurs. Maybe it has something to do with the class definition. I am not sure. The following code is where the namespace errors occur.

Code:
#ifndef BRIDGE
#define BRIDGE
// bridge.h: interface for the Bridge class.

#include <fstream>
#include <iostream>

using namespace std;

class Bridge
{
	 private:
         ifstream theCardFile;

	 public:
		 Bridge(void);           //constructor
		 ~Bridge(void);          //destructor
         void Run(ifstream &theCardFile);
}; //end class Bridge
#endif

////////////////////////////////////////////////////////////
// Construction/Destruction
////////////////////////////////////////////////////////////
Bridge::Bridge()  // error here
{

} // end constructor

Bridge::~Bridge()  // error here
{

} // end destructor

void Bridge::Run(ifstream &theCardFile)  //error here
{ 
    Hand theHand;

    //theHand object is passed the file stream object to its getNextHand method
    //as long is getNextHand returns true, a full 13 card hand is obtained
    while (theHand.getNextHand(theCardFile))
    {   
        /*theHand.sort();
        theHand.score();
        display(theHand);*/
    }
}

Any ideas as to what might create the error.

Thanks,
Todd
 
I'm assuming the implementation is in a .cpp file rather than in the .h file?

Did you forget to include your bridge.h header?
 
Yes I did. The top portion was the *.h file and the bottom was the *.cpp file. Thanks for pointing that out.

Code:
#include "bridge.h"

So it got rid of those 3 errors. I have some more, but I am going to try and get through them.

Todd
 
How about this error: cannot convert parameter 2 from 'char [2]' to 'Card &'.

Code:
bool Hand::getNextHand(ifstream &theCardFile)
{
    //assumes theCards is an array of MAX_CARDS Card elements
    CardReader theReader;
    bool success = false;
    for (int index=0; index < MAX_CARDS; index++)
         success = theReader.getNextCard(theCardFile,theCards[index]);   //error here
    return success;
} //end getNextHand

I'm trying to send the CardFile and theCards[index] array to getNextCard().

Code:
bool CardReader::getNextCard(ifstream &aCardFile, Card &aCard)
{
     char suit = '/0';
     char value = '/0';

     aCardFile >> value >> suit;

     //this test will fail in the event that either the file stream has hit EOF
     //or the data read from the file stream is invalid
     bool success = aCard.set(suit,value);
     return success;
} // end getNextCard

getNextCard() reads input from CardFile and then calls set from the Card class.

Code:
bool Card::set(char suitVal, char faceVal)
{
	bool success = false;
	
	// use strchr() to perform vaidity testing
	if (strchr("CDHS",suitVal) && strchr("234566789AKQJT",faceVal))
    {
		suit = suitVal;
		value = faceVal;
		success = true;
	}

	return success;
} // end Card::set(char suitVal, char faceVal)

There's a good chance I'm missing some code, but I'm not quite sure what it is.

Todd
 
I'm guessing theCards is defined as:
Code:
char theCards[2];
If you change is to be an array of Card objects instead of chars, the error should disappear.

Unlike some of the nasty STL errors you might see later, these types of errors are pretty self explaintory.

"cannot convert parameter 2" -- which you have as theCards[index]

"from 'char [2]'" -- which is what theCards[index] is (or at least what the compiler thinks it is).

"to 'Card &'" -- which of course is the type of the 2nd parameter of getNextCard().
 
So if this is an array of objects, why am I still getting the same compiler error?
Code:
Hand theCards [MAX_CARDS];

I guess I don't get it and I'm using examples I'm finding on the web and they all look like this.
 
OK, in that case theCards is an array of Hand objects, but getNextCard() is expecting a reference to a Card object. It's like sticking a square peg into a round hole. ;-)
 
Got it, finally!

Code:
Card theCards[MAX_CARDS];

I didn't realize what I was looking at since I was thinking it had to be a part of the Hand class and not Card.
 
I have a template function for sorting a hand of cards that was written by the teacher. We have to use it in our project prior to evaluating the hand for point value. The code looks like this:
Code:
template <class ElementType>
void sort(ElementType a[], int numvals)
/*	Purpose: template function to use the insertion
			 sort algorithm to sort in ascending 
			 order an array

	Precondition:	- the operator < must be defined for
					  the ElementType
					- numvals-1 is within the bounds of
					  array a
	Postcondition:	contents of array a are in ascending
					order
*/
{
    int k,j;
    bool done;

    for (k = 1; k < numvals; ++k)  // On pass k, insert entry from
    {                              // index k
        j = k;
        done = false;

        // When (j-1)th element precedes jth element, it is appropriately
        // positioned
        while ((j >= 1) && !done)
            if ((a[j] <a[j-1]))
            {
                ulrich::swap(a[j], a[j-1]);
                --j;
            }
            else               // Know array now sorted in indices 0...k
                done = true;
    }
}
I was told to write something like this to access the sort function.
Code:
sort(theHand,MAX_CARDS);
Well, I have another compiler error that I am not sure of other than maybe I'm not passing the right information to sort.

Code:
c:\Projects\Personal\Csi 250 - c++\Projects\Project1\bridge.cpp(50): error C2784: 'void sort(ElementType [],int)' : could not deduce template argument for 'T1 []' from 'Hand'

Does this error make any sense?
 
I'm not sure how you defined theHand, but I believe is should be an array of Cards.

In the comment, the precondition says operator< must be defined for ElementType. Do you have an operator< function defined for your Card class?

Looking at that code, I can see your teacher is obviously an old C programmer. It's too bad he's reinventing the wheel, since there is already a sort() function in the STL <algorithm> header.
 
This is our first project and we have not gone over anything in the class. So, you can see I am beyond frustrated with him and the fact that he is giving minimal assistance. It's like he's testing us to see how much we know. I can;t say I like it very much either. Anyways,

Code:
Do you have an operator< function defined for your Card class?

I'm not sure if I understand what this means...

theHand should be defined as a hand of cards in the Bridge class. Bridge calls Hand in a while loop, Hand calls CardReader and CardReader calls Card. This is how the hand is populated and once it has 13 cards, then do the sort, calculate the points and display a report inside the while loop. So, the hand of cards would be this, I think, and would have to be passed to the sort method.:

Code:
Card theCards[MAX_CARDS];

Would it be correct that I would have to pass this back to the Bridge class in order to call the sort method? Let me know if I need to give more explanation.

Todd
 
If you look up "Operator Overloading" in your textbook, it should tell you how to write an operator<(), operator=(), operator+() function... Basically, it lets you define what happens when you do something like this:
Code:
Card c1, c2;
...
if ( c1 < c2 )
{
   // Do something.
}

It sounds like he wants you to have a separate Hand class that has an array of 13 Card objects as a member variable...

I'm not quite sure what he means by "calls"? Does he mean "creates"?

This would make a lot more sense to me:
"The Bridge constructor creates n Hands; the Hand constructor calls CardReader::GetCard() 13 times and CardReader::GetCard() returns a Card. After Hand reads 13 cards it calls sort()".
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top