Object = Class Variable

Assign Class Variable to Class Object

Page 1 of 1

6 Replies - 812 Views - Last Post: 15 February 2009 - 06:45 PM Rate Topic: -----

#1 Scorpiobuzz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 06-February 09

Object = Class Variable

Post icon  Posted 15 February 2009 - 02:52 PM

Tring to assign the variable Capital[random_num] to the Class instantiation: Dictionary Dict; Haven't been able to come up with an answer on this and it is driving me nuts.
Here is the Class Dictionary .h file.
#ifndef DICTIONARY_H
#define DICTIONARY_H
#include <string>
#include <iostream>
#include <cstdlib>
#define NUM_READ_LINES 50

using namespace std;

class Dictionary
{
	friend ostream& operator << (ostream&, const Dictionary&);
	friend istream& operator >> (istream&, Dictionary&);
	friend ifstream& operator >> (ifstream&, Dictionary&);
	friend ofstream& operator << (ofstream&, const Dictionary&);

public:
	char Capital[NUM_READ_LINES][50];
	void openFile1();
	int assignment();



	const Dictionary& operator=(const Dictionary&);
	Dictionary(const char *);
	Dictionary();
	Dictionary(const Dictionary&);
	~Dictionary();
	char &operator[] (int);
	const char &operator[](int) const;

//Overload the relational operators for class Dictionary

	bool operator==(const Dictionary&) const;
	bool operator!=(const Dictionary&) const;
	bool operator<=(const Dictionary&) const;
	bool operator<(const Dictionary&) const;
	bool operator>=(const Dictionary&) const;
	bool operator>(const Dictionary&) const;

	char *strPtr; //Pointer to the char array that holds the string
	int strLength; //Variable to hold the length of the string
};
#endif



Here is the Dictionary .cpp file
#include "Dictionary.h"
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cassert>
#include <fstream>
#include <time.h>
#define NUM_READ_LINES 50


using namespace std;

Dictionary::Dictionary(const char *str)
{
	strLength = strlen(str);
	strPtr = new char[strLength +1];

	strcpy(strPtr, str);
}

Dictionary::Dictionary()
{
	strLength = 0;
	strPtr = new char[1];
	strcpy(strPtr, "");
}

Dictionary::Dictionary(const Dictionary& capStr)
{
	strLength = capStr.strLength;
	strPtr = new char[strLength +1];
	strcpy(strPtr, capStr.strPtr);
}

Dictionary::~Dictionary()
{
	delete [] strPtr;
}

const Dictionary& Dictionary::operator =(const Dictionary& capStr)
{
	if (this != &capStr)
	{
		delete [] strPtr;
		strLength = capStr.strLength;
		strPtr = new char[strLength + 1];
		strcpy(strPtr, capStr.strPtr);
	}
	return *this;
}

char& Dictionary::operator [](int index)
{
	assert (0 <= index && index < strLength);
	return strPtr[index];
}

const char& Dictionary::operator [](int index) const
{
	assert (0 <= index && index < strLength);
	return strPtr[index];
}

bool Dictionary::operator==(const Dictionary& capStr) const
{
	return (strcmp(strPtr, capStr.strPtr) == 0);
}

bool Dictionary::operator <(const Dictionary& capStr) const
{
	return (strcmp(strPtr, capStr.strPtr) == 0);
}

bool Dictionary::operator <=(const Dictionary& capStr) const
{
	return (strcmp(strPtr, capStr.strPtr) == 0);
}

bool Dictionary::operator >(const Dictionary& capStr) const
{
	return (strcmp(strPtr, capStr.strPtr) == 0);
}

bool Dictionary::operator >=(const Dictionary& capStr) const
{
	return (strcmp(strPtr, capStr.strPtr) == 0);
}

bool Dictionary::operator !=(const Dictionary& capStr) const
{
	return (strcmp(strPtr, capStr.strPtr) == 0);
}

ostream& operator << (ostream& osObject, const Dictionary& str)
{

	osObject << str.strPtr;

	return osObject;
}

istream& operator >> (istream& isObject, Dictionary& str)
{
	char temp[81];

	isObject >> setw(81) >> temp;
	str = temp;
	return isObject;
}

ifstream& operator >> (ifstream& isObject, Dictionary& str)
{
	char temp[81];

	isObject >> setw(81) >> temp;
	str = temp;
	return isObject;
}
//ofstream& operator << (ofstream& osObject, Dictionary& str)
//{
	//osObject << str.strPtr;

	//return osObject;
//}

void Dictionary::openFile1()
{

	int i = 0;
	int counter = 0;
	ifstream inFile;
	inFile.open("caps.txt");
	while (!inFile.eof() && (i < NUM_READ_LINES))
		{
			inFile.getline(Capital[counter],50);
			counter++;
		}

		for (int i = 0; i < counter; i++) 
		{
			cout << Capital[i] << endl;

		}
}

int Dictionary::assignment()
{
	int random_num;
	srand((unsigned)time(0)); 
	//cout << (rand() % 50 + 1) << endl;
	random_num = (rand() % 50 +1);
	cout << "The randomly chosen word is: " << Capital[random_num] << endl;



	return random_num;
}



And Main .cpp file. The first instantiation of Dict is just to test some of the functions in the class.

#include <iostream>
#include "Dictionary.h"
#include <fstream>

using namespace std;

int main()
{
	Dictionary Dict;
	Dict = "Kansas City";
	cout << "Dict[2] = " << Dict[2] << endl;
	cout << "The length of Dict is: " << Dict.strLength << endl;

	Dict.openFile1();
	Dict.assignment();

	//Here is where everything goes wrong!
	Dictionary Dict2 = Capital[random_num];


	

	


	cin.get();
	cin.get();

	return 0;

}



All I want to do is declare Dict2 is Capital[random_num] so that I can perform the functions in the Dictionary class on the object itself.

Is This A Good Question/Topic? 0
  • +

Replies To: Object = Class Variable

#2 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 119
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: Object = Class Variable

Posted 15 February 2009 - 03:29 PM

You have to access Capital as an instance variable of Dict, "Dict.Capital" Also, rand_num isn't an instance variable of Dict, its the return value from a function. Just change the line to
Dictionary Dict2 = Dict.Capital[Dict.assignment()];



Also, since assignment() returns a value, you don't need to call it above you're problem line. What it does is return the value of a variable, not the variable itself. You have to set something equal to the return value to actually use it.

I get a segmentation fault at the open file part of the program. My file stream stuff is a little rusty, and you might not be getting the error.

Hope this helps!
Was This Post Helpful? 1
  • +
  • -

#3 Scorpiobuzz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 06-February 09

Re: Object = Class Variable

Posted 15 February 2009 - 04:10 PM

That didn't work.The error it gave said the same thing everything else said: " Main.cpp(18) : error C2677: binary '[' : no global operator found which takes type 'std::string' (or there is no acceptable conversion)" I can't figure out why it won't just let me assign it.
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3108
  • View blog
  • Posts: 19,146
  • Joined: 14-September 07

Re: Object = Class Variable

Posted 15 February 2009 - 04:45 PM

That's odd. I haven't run your program, but I can get it to compile just fine. I'll post exactly what I have open in Visual Studio:

Main:
#include <iostream>
#include "Dictionary.h"
#include <fstream>

using namespace std;

int main()
{
	Dictionary Dict;
	Dict = "Kansas City";
	cout << "Dict[2] = " << Dict[2] << endl;
	cout << "The length of Dict is: " << Dict.strLength << endl;

	Dict.openFile1();
	Dict.assignment();

	//This works for me
	Dictionary Dict2 = Dict.Capital[Dict.assignment()];
	return 0;
}




Dictionary.cpp
#include "Dictionary.h"
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cassert>
#include <fstream>
#include <time.h>
#define NUM_READ_LINES 50


using namespace std;

Dictionary::Dictionary(const char *str)
{
	strLength = strlen(str);
	strPtr = new char[strLength +1];

	strcpy(strPtr, str);
}

Dictionary::Dictionary()
{
	strLength = 0;
	strPtr = new char[1];
	strcpy(strPtr, "");
}

Dictionary::Dictionary(const Dictionary& capStr)
{
	strLength = capStr.strLength;
	strPtr = new char[strLength +1];
	strcpy(strPtr, capStr.strPtr);
}

Dictionary::~Dictionary()
{
	delete [] strPtr;
}

const Dictionary& Dictionary::operator =(const Dictionary& capStr)
{
	if (this != &capStr)
	{
		delete [] strPtr;
		strLength = capStr.strLength;
		strPtr = new char[strLength + 1];
		strcpy(strPtr, capStr.strPtr);
	}
	return *this;
}

char& Dictionary::operator [](int index)
{
	assert (0 <= index && index < strLength);
	return strPtr[index];
}

const char& Dictionary::operator [](int index) const
{
	assert (0 <= index && index < strLength);
	return strPtr[index];
}

bool Dictionary::operator==(const Dictionary& capStr) const
{
	return (strcmp(strPtr, capStr.strPtr) == 0);
}

bool Dictionary::operator <(const Dictionary& capStr) const
{
	return (strcmp(strPtr, capStr.strPtr) == 0);
}

bool Dictionary::operator <=(const Dictionary& capStr) const
{
	return (strcmp(strPtr, capStr.strPtr) == 0);
}

bool Dictionary::operator >(const Dictionary& capStr) const
{
	return (strcmp(strPtr, capStr.strPtr) == 0);
}

bool Dictionary::operator >=(const Dictionary& capStr) const
{
	return (strcmp(strPtr, capStr.strPtr) == 0);
}

bool Dictionary::operator !=(const Dictionary& capStr) const
{
	return (strcmp(strPtr, capStr.strPtr) == 0);
}

ostream& operator << (ostream& osObject, const Dictionary& str)
{

	osObject << str.strPtr;

	return osObject;
}

istream& operator >> (istream& isObject, Dictionary& str)
{
	char temp[81];

	isObject >> setw(81) >> temp;
	str = temp;
	return isObject;
}

ifstream& operator >> (ifstream& isObject, Dictionary& str)
{
	char temp[81];

	isObject >> setw(81) >> temp;
	str = temp;
	return isObject;
}
//ofstream& operator << (ofstream& osObject, Dictionary& str)
//{
	//osObject << str.strPtr;

	//return osObject;
//}

void Dictionary::openFile1()
{

	int i = 0;
	int counter = 0;
	ifstream inFile;
	inFile.open("caps.txt");
	while (!inFile.eof() && (i < NUM_READ_LINES))
		{
			inFile.getline(Capital[counter],50);
			counter++;
		}

		for (int i = 0; i < counter; i++)
		{
			cout << Capital[i] << endl;

		}
}

int Dictionary::assignment()
{
	int random_num;
	srand((unsigned)time(0));
	//cout << (rand() % 50 + 1) << endl;
	random_num = (rand() % 50 +1);
	cout << "The randomly chosen word is: " << Capital[random_num] << endl;



	return random_num;
}




Dictionary.h
#ifndef DICTIONARY_H
#define DICTIONARY_H
#include <string>
#include <iostream>
#include <cstdlib>
#define NUM_READ_LINES 50

using namespace std;

class Dictionary
{
	friend ostream& operator << (ostream&, const Dictionary&);
	friend istream& operator >> (istream&, Dictionary&);
	friend ifstream& operator >> (ifstream&, Dictionary&);
	friend ofstream& operator << (ofstream&, const Dictionary&);

public:
	char Capital[NUM_READ_LINES][50];
	void openFile1();
	int assignment();



	const Dictionary& operator=(const Dictionary&);
	Dictionary(const char *);
	Dictionary();
	Dictionary(const Dictionary&);
	~Dictionary();
	char &operator[] (int);
	const char &operator[](int) const;

//Overload the relational operators for class Dictionary

	bool operator==(const Dictionary&) const;
	bool operator!=(const Dictionary&) const;
	bool operator<=(const Dictionary&) const;
	bool operator<(const Dictionary&) const;
	bool operator>=(const Dictionary&) const;
	bool operator>(const Dictionary&) const;

	char *strPtr; //Pointer to the char array that holds the string
	int strLength; //Variable to hold the length of the string
};
#endif



What are you using to build this?

This post has been edited by KYA: 15 February 2009 - 04:45 PM

Was This Post Helpful? 1
  • +
  • -

#5 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 119
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: Object = Class Variable

Posted 15 February 2009 - 06:27 PM

Hold on, I'm not sure if any of us has gotten to this point but I think I have another correction.
char Capital[NUM_READ_LINES][50]; //is the declaration, so shouldn't:

random_num = (rand() % (NUM_READ_LINES +1));  // be the part in the implementation of Dictionary::assignment?


it might not help the problems we have now though, and correct me if I'm wrong.
Was This Post Helpful? 0
  • +
  • -

#6 Scorpiobuzz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 06-February 09

Re: Object = Class Variable

Posted 15 February 2009 - 06:38 PM

Alright crazy and KYA, I don't know how it is you do what you do, but it worked! Dict2 is now Capital[random_num] and I can preform functions on it! WOOT! You two are awesome!

Now, what is this last part crazyjugglerdrummer, what wrong with that piece of code? Are you saying it shouldn't be in the header file for Dictionary?
Was This Post Helpful? 0
  • +
  • -

#7 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 119
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: Object = Class Variable

Posted 15 February 2009 - 06:45 PM

I'm saying that I think you should change the modulus factor in the rand() bit because when you initialized it, you made an array that said there were NUM_READ_LINES of strings, and that each string was 50 chars long. You want a random string, and since there are NUM_READ_LINES strings, that's the maximum number you should get from the rand() thing.

Glad it worked!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1