Trouble setting up and testing a class

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 470 Views - Last Post: 09 July 2012 - 08:28 PM Rate Topic: -----

#1 CoryMore  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 81
  • Joined: 26-June 12

Trouble setting up and testing a class

Posted 09 July 2012 - 11:01 AM

I am attempting to set up and test a class 'color'. It should read in a color as en enumerated variable, and print out the color chosen as well as the int value.

color.h
//color.h
//Color class definition

#ifndef COLOR_H
#define COLOR_H

class color
{
	public:
	enum col {black, blue, green, cyan, red, magenta, brown, lightgray, nocolor};
	
	color();

	//Member functions
	//read color in
	void readColor ();
	
	//write color out
	void writeColor ()const;

	//accessor functions
	void setColor(col);
	int getColor() const;

	private:
	//data members
	enum {capacity = 255};
	col cColor;
	char contents[capacity];
	int length;
};
#endif



color.cpp
#include "color.h"
#include <iostream>
#include <string>
using namespace std;

// Member Functions...
// constructor
color::color()
{
  cColor = nocolor;
}

// Set color
void color::setColor(col c)
{
   cColor = c;
}

void color::readColor()
{
   string c;

   cout << "Please enter a color from the following list: black, blue, green, cyan, red, magenta, brown, lightgray,  or nocolor." << endl;
   cin >> c;
}

// Display attributes
void color::writeColor() const
{
	cout << "color is " << cColor << "or # " << int(cColor) << endl;
}


// accessor functions
int color::getColor() const
{
   return cColor;
}




colorTest.cpp
#include "color.h"
#include <iostream>
using namespace std;

int main()
{
   color myColor();
   

   // Set circle attributes.
   myColor.readColor();
   myColor.setColor(color::magenta);

    // Display the circle attributes.
   myColor.writeColor;
   
   return 0;
}



The first two compile fine, though I'm not sure they're correct other than syntax. I keep getting these errors when trying to compile the test:

Quote

colorTest.cpp: In function `int main()':
colorTest.cpp:14: error: request for member `readColor' in `myColor', which is of non-class type `color ()()'
colorTest.cpp:15: error: request for member `setColor' in `myColor', which is of non-class type `color ()()'
colorTest.cpp:18: error: request for member `writeColor' in `myColor', which is of non-class type `color ()()'


Is This A Good Question/Topic? 0
  • +

Replies To: Trouble setting up and testing a class

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1530
  • View blog
  • Posts: 5,518
  • Joined: 03-August 09

Re: Trouble setting up and testing a class

Posted 09 July 2012 - 11:24 AM

when using the default constructor, you don't add the '()'; otherwise the compiler thinks your declaring a prototype for a function named 'myColor' that returns 'color' and accepts no arguments.

color myColor();
should be...
color myColor;

This post has been edited by ishkabible: 09 July 2012 - 11:25 AM

Was This Post Helpful? 1
  • +
  • -

#3 CoryMore  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 81
  • Joined: 26-June 12

Re: Trouble setting up and testing a class

Posted 09 July 2012 - 11:47 AM

I see, I've adjusted accordingly, but it still will not compile correctly.

#include "color.h"
#include <iostream>
using namespace std;

int main()
{
   color myColor;
   

   // Set circle attributes.
   myColor.readColor();
   myColor.setColor(color::col);

    // Display the circle attributes.
   myColor.writeColor;
   
   return 0;
}



Getting the errors:

Quote

colorTest.cpp: In function `int main()':
colorTest.cpp:15: error: expected primary-expression before ')' token
colorTest.cpp:18: error: statement cannot resolve address of overloaded function

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1938
  • View blog
  • Posts: 5,770
  • Joined: 05-May 12

Re: Trouble setting up and testing a class

Posted 09 July 2012 - 11:54 AM

Don't you want line 12 to be myColor.SetColor(color::magenta); like in your original colorTest.cpp?
Was This Post Helpful? 0
  • +
  • -

#5 CoryMore  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 81
  • Joined: 26-June 12

Re: Trouble setting up and testing a class

Posted 09 July 2012 - 11:59 AM

I probably do for the test, but I'll need it to be input by the user in the final program, so I thought I'd attempt to test that here.

Changing it to:
#include "color.h"
#include <iostream>
using namespace std;

int main()
{
   color myColor;
   

   // Set circle attributes.
  
   myColor.setColor(color::magenta);

    // Display the circle attributes.
   myColor.writeColor();
   
   return 0;
}



Did work. Now I just need to figure out how to get the user to input the fields.
Was This Post Helpful? 0
  • +
  • -

#6 CoryMore  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 81
  • Joined: 26-June 12

Re: Trouble setting up and testing a class

Posted 09 July 2012 - 12:12 PM

When I try to assign a variable to
myColor.setColor(color::magenta);


By making it
myColor.setColor(color::c);



It doesn't work, any tips on making this user input instead of static?
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1938
  • View blog
  • Posts: 5,770
  • Joined: 05-May 12

Re: Trouble setting up and testing a class

Posted 09 July 2012 - 12:43 PM

What do you mean by "it doesn't work"? Can you post your new code? What is color::c?

Right now, if I put on my Kreskin the clairvoyant hat, I'm guessing that what you mean by "it doesn't work" is that you are getting a compile time error, and that the error is complaining about "color::c". I would say that you are getting that error because there is no "c" defined in the color class. That is what you are basically telling the compiler when you say "color::c" -- "access 'c' in class 'color'".
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1938
  • View blog
  • Posts: 5,770
  • Joined: 05-May 12

Re: Trouble setting up and testing a class

Posted 09 July 2012 - 12:57 PM

As for getting the user to enter the color, there are a couple of approaches:
Option 1 - make the user do all the work.
Step 1: Print out a list with the color names and the corresponding enum values for the colors. Step 2: Have the user enter the number of the color and store it in an int.
Step 3: Cast the int into a color::col and call setColor();

Option 2 - translate strings to enums in your code.
Step 1: Create a mapping between the string name of a color and its enum value. (It can be a simple array, or as complicated as using std::map.)
Step 2: Print out a list of color name strings.
Step 3: Have the use enter one of the valid names.
Step 4: Look for the corresponding enum value in the mapping.
Step 5: Call setColor() with the enum value you found.
Was This Post Helpful? 1
  • +
  • -

#9 CoryMore  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 81
  • Joined: 26-June 12

Re: Trouble setting up and testing a class

Posted 09 July 2012 - 03:05 PM

I'd like to do it the second way, as I think that's what the project requires. But I don't think my switch statement handles a string. And I'm not sure I'm savvy enough to do the mapping etc. Here's where I'm at the first way.

#include "color.h"
#include <iostream>
#include <string>
using namespace std;

// Member Functions...
// constructor
color::color()
{
  cColor = nocolor;
}

// Set color
void color::setColor(col c)
{
   cColor = c;
}

color::col color::readColor()
{
  // Local data ...
   int colorInt;          // storage for data character

   // Loop until a valid character is read.
   do
   {
      cout << "Enter one of the values for a color: "<< endl;
	cout << "0 - black, 1 - blue, 2 - green, 3 - cyan, 4- red, " << endl;
	cout << "5 - magenta, 6 - brown, 7 - lightgray, 8 - nocolor: "<< endl;
      cin >> colorInt;
      
      // Return color value if valid character read.
	switch(colorInt)
	{
	 case 0 : return black;
         case 1 : return blue;
         case 2 : return green;
         case 3 : return cyan;
	 case 4 : return red;
         case 5 : return magenta;
         case 6 : return brown;
         case 7 : return lightgray;
	 case 8 : return nocolor;
         default  : cout << "Try again!" << endl;
      } // end Switch
   } while (true);
}

// Display attributes
void color::writeColor(col thisColor)
{
	switch(thisColor)
	{
      case black:
         cout << "black";
         break;
      case blue:
         cout << "blue";
         break;
      case green:
         cout << "green";
         break;
      case cyan:
         cout << "cyan";
         break;
      case red:
         cout << "red";
         break;
      case magenta:
         cout << "magenta";
         break;
      case brown:
         cout << "brown";
         break;
      case lightgray:
         cout << "lightgray";
         break;
      case nocolor:
         cout << "nocolor";
         break;
      default:
         cout << "*** ERROR: Invalid color for value."
              << endl;
	}
}


// accessor functions
int color::getColor() const
{
   return cColor;
}


Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1938
  • View blog
  • Posts: 5,770
  • Joined: 05-May 12

Re: Trouble setting up and testing a class

Posted 09 July 2012 - 03:38 PM

Here is some quick and dirty code to show the concept of using an array:
#include <iostream>
#include <string>

using namespace std;

enum Muppets { Kermit, Beaker, Fozzy, MissPiggy, MuppetMax };

// Keep these in sync with the Muppets enum
static const char * MuppetNames[] =
{
    "Kermit",
    "Beaker",
    "Fozzy",
    "Miss Piggy",
};

Muppets MuppetNameToEnum(const char * name)
{
    int i;
    for(i = (int) Kermit; i < (int) MuppetMax; i++)
    {
        // case insensitive search
        if (0 == strcmpi(name, MuppetNames[i]))
            break;
    }
    return (Muppets) i;
}

const char * MuppetEnumToName(Muppets muppet)
{
    if (muppet < Kermit || muppet > MuppetMax)
        return nullptr;
    return MuppetNames[muppet];
}

int main()
{
    char buffer[256];
    cin.getline(buffer, sizeof(buffer));
    Muppets muppet = MuppetNameToEnum(buffer);
    const char * found = MuppetEnumToName(muppet);
    if (found)
        cout << "You picked " << found << " with an enum value of " << (int) muppet << endl;
    else
        cout << "No matching muppet found." << endl;
}


Was This Post Helpful? 1
  • +
  • -

#11 CoryMore  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 81
  • Joined: 26-June 12

Re: Trouble setting up and testing a class

Posted 09 July 2012 - 03:54 PM


#include "rectangle.h"
#include "color.h"
#include <iomanip>
#include <iostream>
using namespace std;

int main()
{
	double length;
	double width;
	int x;
	int y;
	

  	color myColor;
	color::col shapeColor;

	rectangle myRectangle(l,w);
 	double area, perimeter;

  	 // Set rectangle attributes.
	cout << "Please enter starting x and y coordinates: " << endl;
	cin >> x >> y >> endl;
   	myRectangle.setCoord(x, y);
	
	 	
   	// Compute area and perimeter
   	area = myRectangle.computeArea();
   	perimeter = myRectangle.computePerimeter();

	// Set color attributes.
  	 shapeColor = myColor.readColor();

  	 // Display the rectangle attributes.
  	 cout << "The rectangle attributes follow:" << endl;
   	myRectangle.displayRectangle();
   
   	// Display area and perimeter.
  	 cout << "area is " << area << endl;
   	cout << "perimeter is " << perimeter << endl;
	
 	// Display the color attributes.
  	 myColor.writeColor();

}



I'm getting the following error:
rectangleImp.cpp:31: error: no match for 'operator>>'
Was This Post Helpful? 0
  • +
  • -

#12 CoryMore  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 81
  • Joined: 26-June 12

Re: Trouble setting up and testing a class

Posted 09 July 2012 - 04:05 PM

That looks amazing, and frankly, I wish I could do it, but it is still beyond me. I appreciate the advice though, and in my spare time will try to make it work with the code I have.
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1938
  • View blog
  • Posts: 5,770
  • Joined: 05-May 12

Re: Trouble setting up and testing a class

Posted 09 July 2012 - 04:46 PM

View PostCoryMore, on 09 July 2012 - 03:54 PM, said:

I'm getting the following error:
rectangleImp.cpp:31: error: no match for 'operator>>'

Line 31 was a comment line. Are you sure you posted the right code?

Ignoring the line number, it looks like you have an extra ">> endl" on line 23.

This post has been edited by Skydiver: 09 July 2012 - 04:48 PM

Was This Post Helpful? 1
  • +
  • -

#14 CoryMore  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 81
  • Joined: 26-June 12

Re: Trouble setting up and testing a class

Posted 09 July 2012 - 06:03 PM

That endl was messing it up, it is resolved now, thank you. I wish I could work out that bit of code to make the strings work in my switch statement though.
Was This Post Helpful? 0
  • +
  • -

#15 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1938
  • View blog
  • Posts: 5,770
  • Joined: 05-May 12

Re: Trouble setting up and testing a class

Posted 09 July 2012 - 06:32 PM

C++ currently doesn't know how to handle strings for switch statements.

If you are really bent on using a switch statement you could do something like:
int string_hash(const char * s)
{
    // compute some hash for the string s.
}

switch(string_hash(input))
{
case HASH_FOR_BLACK:
    return black;
case HASH_FOR_BLUE:
    return blue;
:
}



Where HASH_FOR_xxx are constant integers that you get from having called string_hash("xxx");


The most brute force approach is simply have a chain of if statements:
if (strcmpi(input, "black") == 0)
    return black;
if (strcmpi(input, "blue") == 0)
    return blue;
:



The loop I presented in MuppetNameToEnum() simply packs that chain of if statements into a loop and made it data driven by reading the string from an array instead of hard coded strings on each line.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2