How to create array of my own class and access it

Hello, I have created class, that contains several strings. It's minor version is:

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
27
#include <iostream>
#include <string>
using namespace std;

class Opis{
	private:
	string people;
	string structures;
	string ways;
	public:
	//Constructors
    Opis() {}
    Opis(string pe){people = pe;}//Short version of long constructor
    Opis(string pe, string st, string way)
{assign(pe, st, way);}
Opis operator=(string pe){people = pe;}
	void assign(string pe,string st,string way);
	string show_pe() const {return people;}
	string show_st() const {return structures;}
	string show_way() const {return ways;}


void Opis::assign(string pe,string st,	string way,) {
	people = pe;
	structures = st;
	ways = way;
}

So mainly that's the class, it has also friend ostream << operator, but since it works well and it requires a bit of space, i won't insert it.

My problem is, that I want to implement this in other program. Next program is imho way too long to instert it here. Anyway, here's my problem:

Second program includes "Opis.cpp"(previous code). Code fits well, no problem with that, but my second program has two int variables: X and Y, and they change depending on user's input. I want my program to call Opis depending on X and Y. I wanted to create array of Opis(something like Opis name[30][30], and then assign to name[0][0] something like this:

name[0][0]("People funny");

But it doesn't work. Compiler shows thiss message:

no match for call to '(Opis)(const char[13])'

I wanted to assign many various strings to name, so I could use something like cout<<name[x][y] and it would show the result as I want, but I can't create array of my class correctly. Any help?:)

Thanks in advance!
The compiler message says that you don't have a constructor that takes a thirteen-byte character array. So either add one, or make the argument a string when you call the constructor:

1
2
3
4

name[ 0 ][ 0 ]( string( "1234567890123" ) );



Last edited on
closed account (zb0S216C)
MatthewRock wrote:
name[0][0]("People funny");

This calls Opis::operator()(), which you haven't overloaded. You can access the objects like so:

 
name[0][0].Member_of_the_Class;

You'll need the dot operator to access the members. If name is an array of pointers to Opis objects, then you'll need the arrow operator. For instance:

1
2
3
Opis *Instances[2][2] = { ... };

Instance[0][0]->Member_of_the_Class;

The arrow operator is the readable version of: (*Pointer).Member_of_the_Class;

Wazzak
Last edited on
So I understand that I haven't created constructor for arrays. How should it look like?
Opis(const char * pPeople) : people(pPeople){}
@clanmjc Isn't that C?
Your compiler said...
no match for call to '(Opis)(const char[13])'


This is the matching call
Opis(const char * pPeople) : people(pPeople){}


It's the same thing you get when you call strings c_str() method, so I guess you could call it a 'C' string :P But it's used all over c++, especially when portability is concerned.
Maybe I'm just retarded, but I don't understand the solution. Could someone explain it to me on begginer-level?
C++ is a superset of C. Most of the code you are writing is C code. Classes is a C++ feature. But, most of the code you write in a class can be considered plain old C code.

The operator overloaded function in your Opis class take a 'string' type variable.
Opis(string pe)

name[0][0]("People funny");
"People funny" is not a 'string' type variable. It is an array of 'char' type.

So, look at what kooth said. You can easily do this:
name[0][0](string ("People funny"));
This converts the array of 'char' to a 'string' which is what your function wants.
Note:

You can do what clanmjc said.
Opis(const char * pPeople) : people(pPeople){}
This is how you make a constructor to accept an array of 'char' but then you will have a problem.

The 'string' type is like an array of 'char' and the compiler won't know which constructor to use. Depending on your compiler, it might work, it might not.

The best option is to convert raw text ( "this is raw text" ) to a 'string' type.
string ("this is raw text being converted to a 'string' type")
Last edited on
(Note I am assuming you're talking about pre-c++11 arrays. The new uniform initialization coming in with c++11 is changing these rules)

When constructing an array of objects, each element is constructed using the default constructor, which you have already provided. You cannot use a brace initializer list, like you can for C-style, POD structs.

In your case, while you don't have an operator(), you do have an operator=, so you should be able to do the following.

1
2
3
4
5
6
7
8
9
10
11
12
13
...

int main()
{
	Opis name[30][30]; // all constructed with the default constructor

	name[0][0] = "Bill"; // assign name to element 0, 0
	// or you could use e.g. name[0][0].assign("Bill", "foo", "bar");

	cout << name[0][0].show_pe() << endl;

	return 0;
}


Andy

Last edited on
Hmm... I made something like this:

1
2
3
4
5
6
...


Opis name[30][30];
name[0][0].assign(/*strings to assign*/);


And apparently it works.
I don't know if I hadn't tried it before or i just changed something, but you guys aided me at this, thank you very much. Now i can go further:)
Why does it work? void assign(string pe,string st,string way)
Because you have code for it to work. Lol.
Topic archived. No new replies allowed.