What's wrong with my Overload Constructors?

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;
}
I made some notes on your MyClass class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class MyClass
{
private:
	char* a;
	char* b;
public:
	MyClass(char noun[]) // I think you meant this
	: a(0), b(0) // recommend zeroing your pointers
	{
		if(a = new char[strlen(noun) + 1])
			strcpy(a, noun);
	}

	// Don't forget to clean up
	~MyClass()
	{
		delete[] a;
		delete[] b;
	}

	char* displayNoun()
	{
		return a;
	}

};


I would recommend replacing your char* members with std::string.
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;
}
What are these lines supposed to achieve?

1
2
	MyClass A(char noun);
	MyClass2 B(char verb);
It' supposed to pass the the values (nouns and verbs) from whatever the user enters.
Topic archived. No new replies allowed.