classes in an array of a structure

Started by
10 comments, last by Densun 23 years, 11 months ago
This post is similar to one I recently made, but this is a better example of my situation. I have a class object in a structure like this: class CTest { CTest () {} ~CTest () {} long num; }; typedef struct test_s { char* name; long id; CTest test; } test_t; test_t tests[2] = { { "name", 16, }, { "name2", 17, } }; I get this error: initializing' : cannot convert from 'char [10]' to 'struct test_s'. No constructor could take the source type, or constructor overload resolution was ambiguous. I think it has something to do with the constructor of the class. Edited by - Densun on 5/6/00 6:13:33 PM
Advertisement
just a question: how come you embed a class in a struct. it would seem simpler to embed a class in a class. i am not trying to offend, just asking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
Well there are a few things wrong with your class so let me show you how to do it corretcly.


class CTest
{
private:

char *stringone;

public:

// Our class constructor.
CTest(char *s);

// Our default class constructor.
CTest();

// Our default class destructor.
~CTest();

// Copy Constructor.
CTest ();

// Assignment operator.
CTest operator=(const CTest &c);

// Returns the current string value.
char *returnname(char *str);

};

--------------------------------------------------

// Our class constructor.
CTest(char *s)
{
// Get the size of the input string.
int stringsize = sizeof(s);

// Create a new string.
char string = new char[stringsize + 1];

// Copy the information into that string.
strcpy(string,s);
}




// Our default class constructor.
CTest()
{
// set a default value to copy into the string.
char a= "\0";

// Create a new string
char *string = new char[1];

// copy the a value into the new string.
strcpy(string,a);

}


// Copy Constructor.
CTest (CTest &c)
{
// Get the size of the string.
int strsize=sizeof(c.string);

// Create a new string to hold the information.
char *string = new char[strsize + 1];

// Copy the old string to the new string.
strcpy(string,c.string);

}

// Assignment operator.
CTest operator=(const CTest &c)
{
// Get the size of the string.
int strsize=sizeof(c.string);

// Create a new string to hold the information.
char *string = new char[strsize + 1];

// Copy the old string to the new string.
strcpy(string,c.string);

return this;

}

// Returns the current string value.
char *returnname(char *str)
{
return string;
}

~CTest()
{
delete string;
}
----------------------------------------------------

Now to use this follow this code:

typedef struct test_s
{
CTest test;
long id;
} test_t;

test_t tests[2] =
{
{ test("name"),16 },
{ test("name2"), 17 }
};






Edited by - evaclear on 5/6/00 7:27:05 PM
Joseph FernaldSoftware EngineerRed Storm Entertainment.------------------------The opinions expressed are that of the person postingand not that of Red Storm Entertainment.
Is it just me or are you doing something like this in your code:

test_t tests[2] = {"name", // =name  (OK)16,     // = id   (OK)},{"name2",  // =test (WRONG because constructor does not           // take char* as parameter)...}}; 


Don´t you need an extra "," after the first id part:

test_t tests[2] = {"name", // =name  OK16,     // = id   OK},      // extra ",", so test can use constructor ,  .... 


Hope this will work.
The code I put in my post is not the actual code I''m using. I just made the class and structure up, but it works the same way I''m trying to do what I am.

The actual application of this is creating a global array of frames that can be used for sprites in my engine. The reason I have a class is because most of the code is C++ objects, but it was easier to create a C structure for this.

Anonymous Poster: adding the comma after the id does not help. I''m just intializing the first two variables, and leaving the rest of the structure to be whatever it was.

Something that will work is creating a pointer to the class in the structure. When I create the array, I can allocate memory for it.
I don''t have the answer to your original question, because I''m not on a compiler right now. But I wanted to clear a few things up real quick, becuase at least 2 posters in this thread have made slightly incorrect and misleading statements.

First, for the poster who though putting a class in a class is different than putting a class in a struct. You are incorrect. In C++ the follwing two examples are identical:

Class A
{
public:
// stuff here
};

Struct A
{
// stuff here
};

So it make NO difference if you use a class or struct AS LONG AS you put everything under the appropriate access specifier (public, private, protected). You can even inheirit structs from each other, AND from classes, struct is simple a SYNONYM for class with public access by default. It exists solely for C compatibility.

This is related to my second note. The poster who used a line like this:

typedef struct test_s
{
//stuff
}; test_t;

this is absolutely silly in C++ and even a little misleading. In C, a struct did NOT by default define a type name, so the typedef was necessary for structs. In C++ the name follwing the keyword ''class'' or ''struct'' is the typename, and no furthur typedef is necessary. This is a C programming idiom, that should NOT be used in a C++ program, because it makes the user think it its a C program, but since the struct contains a C++ class, it CANNOT be compiled on C compilers, so shouldn''t suggest that it should.

Which brings me to point number three, a small somewhat nit-picky point. Denson''s last post suggested that he created a ''C'' struct because it was quicker than a full fledged C++ class, but that is NOT in fact what he created. He created a C++ struct, which is 100% incompatible with C, and indead he could just as easily make it a C++ class, with public data members until such time as he replaces then with his desired access functions (although if they are going to remain public due to their nature and not laziness, I too use C++ structs for these types, nothing wrong with bundling up types into a structure, to ease allocation and argument passing).

If someone would post how to make code look right in a post, i would give you my best attempt at answering your first question. But I have not yet figured out how to keep angle brackets and underscores.
Well, you can''t initialise your class like that. It has a private member which you can only set up through a constructor or a member function, and you can''t call that member function in that kind of initialisation. If you comment out the CTest member from your struct, does the initialisation work? Either way, you are only passing 2 initialisers to a structure that contains 3 members, which will probably make it choke at some point.
Xai: Densun''s moving to C++ from C so lighten up a bit! (Also, just click the edit button on my post to see how I did the code below.)

Kylotan: Even a class with no data members and just a public default constructor doesn''t work. If you take out all the member functions it works fine, probably because of some old C rules about structs-in-structs...


Anyway, the problem is the constructor, which should be able to be called like this:


test_t tests[2] =
{
{
"name",
16,
CTest(),
},

{
"name",
17,
CTest()
}
};



But that default constructor should be called regardless of whether it is specified, shouldn''t it?


- null_pointer
Sabre Multimedia
Try this:

struct test_t{  char* name;  long id;  CTest test;  test_t(char* c, long l) { name = c; id = l; }}; 



test_t tests[2] = { test_t("name", 16), test_t("name2", 17) }; 


The constructor for test_t will call the default constructor for CTest.
quote:Original post by null_pointer

Kylotan: Even a class with no data members and just a public default constructor doesn''t work. If you take out all the member functions it works fine, probably because of some old C rules about structs-in-structs...


Well, if you have an empty class, what''s to initialise?
I rarely do this sort of thing so I''m hardly an expert, but from my own experiences if you make all the members public, it works just as if it was a struct in C, member functions or not. Only tested on MSVC though.

This topic is closed to new replies.

Advertisement