Jul 23, 2010 at 9:27pm UTC
I'm just starting out with classes in C++, and for this program, my compiler is saying my overload constructors that I have set up in the classes are incorrect. It says: "Error, an array may not have elements of this type." Basically my program is supposed to ask the user for 10 nouns and 10 verbs, and then randomly display sentences with the nouns and verbs. I tried doing this in classes, but don't know what I'm doing wrong? This is what I have so far...
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cmath>
using namespace std;
class MyClass
{
private:
char* a;
char* b;
public:
MyClass (char noun [][] ) {
if(a = new char [strlen(noun) + 1])
strcpy (a, noun);
}
char* displayNoun() {return a;}
};
class MyClass2
{
private:
char* b;
public:
MyClass2 (char verb [][]) {
if(b = new char [strlen(verb) + 1])
strcpy (b, verb);
}
char* displayVerb() {return b;}
};
int main()
{
char* insuffMem = "Insufficient Heap memory Available";
int num;
char noun[10][10], verb[10][10];
cout<<"Enter 10 nouns: ";
for(int i=0; i<10; i++)
cin>>noun[i];
cout<<"Enter 10 verbs in the third person singular: ";
for(int j=0; j<10; j++)
cin>>verb[j];
//ask user how many sentences they want to generate
cout<<"How many sentences do you want to generate? ";
cin>>num;
MyClass A(noun);
MyClass2 B(verb);
if(A.displayNoun() || B.displayVerb() == NULL) // Test if Heap allocated for copy of string
{ cout<<insuffMem <<endl;
exit (1);}
else
{
srand(time(NULL));
for(int i=0; i<num; ++i)
cout <<"The " << A.displayNoun()[rand()%10] <<" " << B.displayVerb()[rand()%10] << " the " << A.displayNoun()[rand()%10] <<endl;
}
system("pause");
return 0;
}
Jul 24, 2010 at 1:17am UTC
Ok so I changed it around, but it won't output my displayNoun or displayVerb methods. It says: error, request for member 'displayNoun' in 'A', which is of non-class type 'MyClass(char)'
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cmath>
using namespace std;
class MyClass
{
private:
char* a;
public:
MyClass(char noun[]) // I think you meant this
// recommend zeroing your pointers
{
if(a = new char[strlen(noun) + 1])
strcpy(a, noun);
}
// Don't forget to clean up
~MyClass()
{
delete[] a;
}
char* displayNoun()
{
return a;
}
};
class MyClass2
{
private:
char* b;
public:
MyClass2 (char verb[]) // I think you meant this
// recommend zeroing your pointers
{
if(b = new char[strlen(verb) + 1])
strcpy(b, verb);
}
// Don't forget to clean up
~MyClass2()
{
delete[] b;
}
char* displayNoun()
{
return b;
}
};
int main()
{
char* insuffMem = "Insufficient Heap memory Available";
int num;
char noun[10][10], verb[10][10];
cout<<"Enter 10 nouns: ";
for(int i=0; i<10; i++)
cin>>noun[i];
cout<<"Enter 10 verbs in the third person singular: ";
for(int j=0; j<10; j++)
cin>>verb[j];
//ask user how many sentences they want to generate
cout<<"How many sentences do you want to generate? ";
cin>>num;
MyClass A(char noun);
MyClass2 B(char verb);
if(A.displayNoun() || B.displayVerb() == NULL) // Test if Heap allocated for copy of string
{ cout<<insuffMem <<endl;
exit (1);}
else
{srand(time(NULL));
for(int i=0; i<num; ++i)
cout <<"The " << A.displayNoun()[rand()%10] <<" " << B.displayVerb()[rand()%10] << " the " << A.displayNoun()[rand()%10] <<endl;
system("pause");
return 0;
}
Jul 24, 2010 at 1:47am UTC
It' supposed to pass the the values (nouns and verbs) from whatever the user enters.