Jump to content
  • Advertisement
Sign in to follow this  
justcallmedrago

char's and char*'s in structs

This topic is 3818 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Scet
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.


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 this post


Link to post
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 string
int count = myString.length();
// count should now hold the value 35

// or you can get a sub-string of the original
std::string mySubString = myString.substr(17)
// mySubString should now contain "tains the name Bob" or so

return 0;
}




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

Share this post


Link to post
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) operator
Object Object1;
Object1.name= "name here";

or

//Object is a pointer so we use the ->(pointer) operator
Object* Object1;
Object1->name= "name here";

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!