returning structs across classes

Started by
14 comments, last by rip-off 13 years, 4 months ago
I am trying to pass a few variables from one class to another using structs as opposed to arrays as suggested.

When I attempt to create the function in my cpp file it tells me that a ; is required?

My header file has a struct with the function prototype

class test{public:typedef struct NormalArray{	float normX;	float normY;	float normZ;}SAMPLE_STRUCT;	//function prototype	SAMPLE_STRUCT GetNormal();private:};

I am then trying to use this in my cpp file. The cpp file can find the fields and items associated with the struct and seems to be OK except it wont compile.
#include "test.h"	SAMPLE_STRUCT GetNormal()	{ //need ; here?				SAMPLE_STRUCT sample_struct;		sample_struct.normX = 5;		sample_struct.normY = 12;		sample_struct.normZ = 20;		std::cout<<"sample struct is "<<sample_struct.normX<<std::endl;		return sample_struct;	}


I am just trying to use a really simple example just to put the values in print the mout then return the struct. I want to them print this out again to ensure that the values are passed correctly etc.

Do I need to pass by reference using pointers? Am I missing something obvious?

Many thanks
Advertisement
Identifiers that live inside the test class need to be prefixed with test:: when used outside of the class.
SAMPLE_STRUCT test::GetNormal(){   // etc}


When you define the body of a class member outside of the class, you need to prefix the method name with the class name and the :: operator.
There may be more going on here than this, but in your .cpp file, both SAMPLE_STRUCT and GetNormal() need to be qualified, i.e.:
test::SAMPLE_STRUCT test::GetNormal()
Also, there shouldn't be any need to use the 'typedef struct' idiom here.
You need to let the compiler know where GetNormal comes from. Imaging you have several different classes, all with a member method called GetNormal. Which one are you referring to? Same for SAMPLE_STRUCT when you use it outside "test". So change your cpp to
	test::SAMPLE_STRUCT test::GetNormal()	{ //need ; here?				SAMPLE_STRUCT sample_struct;		sample_struct.normX = 5;		sample_struct.normY = 12;		sample_struct.normZ = 20;		std::cout<<"sample struct is "<<sample_struct.normX<<std::endl;		return sample_struct;	}

Adding "test::" lets the compiler know where the method or type comes from, so to speak.
A Normal is a well defined type on its own. I would argue you should make it a top level type:
struct Normal{   float x;   float y;   floay z;};

Note that "typedef struct { ... } whatever" idiom is unnecessary in C++. Also avoid using redundant prefixes on member names. A good type name, like "Normal" above, makes it clear.

Compare:
Normal normal;// Needless prefixesfoo(normal.normX, normal.normY, normal.normZ); // bit wordy// No prefixesfoo(normal.x, normal.y, normal.z); // nice!
I have renamed the struct and have took out the typeDef

I have know tried to use the accessors as you suggested
Test::NormalArray Test::GetNormal() //I get an error telling that function cannot be redeclared outside of the class? { }


If I do Test::NormalArray:: it finds my X,Y,Z values but I cant get it to allow me to do the actual function?
Post the actual code and the actual error messages please.
header file
#pragma once#include <windows.h>		// Header File For Windows#include <gl\gl.h>			// Header File For The OpenGL32 Library#include <gl\glu.h>			// Header File For The GLu32 Library#include "console.h"class test{public:struct NormalArray{	float x;	float y;	float z;};	//function prototype	static NormalArray GetNormal();private:};


cpp file
#include "test.h"Enviroment::NormalArray Enviroment::GetNormal() 	{		NormalArray test;		test = 5;		sample_struct.normY = 12;		sample_struct.normZ = 20;		std::cout<<"sample struct is "<<sample_struct.normX<<std::endl;		return sample_struct;	}


I get the error
140 IntelliSense: member function "Enviroment::GetNormal" may not be redeclared outside its class c:\users\phil\desktop\game draft 3\win32opengl2template\enviroment.cpp 132 38 win32template

I now get errors to do with accessing test but I am assuming thats because of the other error.

Just read my error message it may still be linking to my other stuff I'll start again and see what happens
Why are you using Environment:: when your class is named test?

This topic is closed to new replies.

Advertisement