char's and char*'s in structs

Recommended Posts

-I'm using C++ -I'm using DirectX I am having trouble putting an object's name in a character array inside of it. This is what I'm trying. It's not compiling. (Global Declarations):
struct Object{
char Name[30];
LPD3DXMESH Mesh;
D3DMATERIAL9* Material;
LPDIRECT3DTEXTURE9* Texture;
DWORD numMaterials;
};

Object Object1;
Object1->Name = "airplane 2.x"; //LINE 32


I get the errors:
Quote:
 main code.cpp(32) : error C2143: syntax error : missing ';' before '->' main code.cpp(32) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int main code.cpp(32) : error C2371: 'Object1' : redefinition; different basic types main code.cpp(31) : see declaration of 'Object1'
I'm trying to be able to pass just a pointer to an "Object" rather than all it's parts.

Share on other sites
Quote:
 Object Object1;Object1->Name = "airplane 2.x"; //LINE 32

Object1 is not a pointer, so don't use -> to access its members. Do this:
Object1.Name = "airplane 2.x";

Quote:
 -I'm using C++

C++ has a string type, so you don't need to fool about with arrays of characters. Use a std::string.

Share on other sites
Plus, with the string class, you don't have to worry about length, or wasting memory due to over-zealous array lengths.

Share on other sites
Well the reason you're getting those errors, is that your assignment is outside of a function. Assignments must be either where a variable is declared or in a function(like everything else).

It should be:

Object Object1 = { "airplane 2.x" };

or

Object Object1 = { .Name = "airplane 2.x" };

or since this is C++ and not C, just make a constructor for Object and pass in the name.

Share on other sites
Quote:
 Original post by ScetWell the reason you're getting those errors, is that your assignment is outside of a function. Assignments must be either where a variable is declared or in a function(like everything else).It should be:Object Object1 = { "airplane 2.x" };orObject Object1 = { .Name = "airplane 2.x" };or since this is C++ and not C, just make a constructor for Object and pass in the name.

Constructors! Of course! thank you. I've heard of these before but hadn't dealt with structs/classes much. I will look into them now.

Much thanks.

Share on other sites
Ok, I'd like to use std::string. Where can I start? I went to MSDN at their documentation and it didn't help :/

Share on other sites
First you'll need to include the appropriate headers, then its just a matter of using the string class type.

For example:

#include <string>std::string myString("a string that contains the name Bob");int myFunc(){// Now you can do things like get the how many chars are in the stringint count = myString.length();// count should now hold the value 35// or you can get a sub-string of the originalstd::string mySubString = myString.substr(17)// mySubString should now contain "tains the name Bob" or soreturn 0;}

Reference books/documentation/websites come in handy, such as http://www.cppreference.com/cppstring/index.html

Share on other sites
to use std::string you have to include the head file <string>

change
char name[20];

to
std::string name;

the reason your getting the error is simply because the -> operator is for pointers since your not defining Object1 as a pointer you have to use the . operator

//Object is not a pointer so we use the .(dot) operatorObject Object1;Object1.name= "name here";or//Object is a pointer so we use the ->(pointer) operatorObject* Object1;Object1->name= "name here";

Create an account

Register a new account

• Forum Statistics

• Total Topics
628354
• Total Posts
2982236

• 10
• 9
• 11
• 24
• 11