Jump to content
  • Advertisement
Sign in to follow this  
eskimo456

returning structs across classes

This topic is 2927 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 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

Share this post


Link to post
Share on other sites
Advertisement
Identifiers that live inside the test class need to be prefixed with test:: when used outside of the class.

Share this post


Link to post
Share on other sites

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.

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
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 prefixes
foo(normal.normX, normal.normY, normal.normZ); // bit wordy

// No prefixes
foo(normal.x, normal.y, normal.z); // nice!

Share this post


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

Share this post


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

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!