PDA

View Full Version : Struct in a class



Atomic_Sheep
30th January 2012, 09:25
Hi, I defined a struct in the default ui header file.

In the clicked slot of the ui class, I assigned values to various struct members based on inputs received from the ui:

e.g. struct.name = ui->lineedit->text();

How do I access struct.name from a different class? I need to be able to compare the struct.name value to a string or int for example. Is it even possible to access things from a slot?

wysota
30th January 2012, 10:06
This is a classic C++ problem, it has nothing to do with Qt. If you want to access object (not struct, you don't access structs but instances of it) B from A then B needs to be visible to A using any of the mechanisms provided by C++ (like passing pointers around).

Atomic_Sheep
9th February 2012, 15:04
This is driving me insane... as I said, I created a struct within a class and assigned values to them, I then passed that struct... well the pointer to another function:


//creating a new object
object object;

object.fileopen(&variables);

in the other .cpp file which contains the function of fileopen, here's the code that I have:


void FileManager::fileopen(struct properties *pointer)
{

//code

}

However, I'm getting an error at compile:

extra qualification 'File Manager' on member function 'fileopen'

I included a function prototype in the header file for the filemanager.cpp file:

void FileManager::fileopen(struct properties *pointer);

What does this error mean? I checked the {} brackets, all seem to be in order. Either I'm using the pointers wrong or something else is happening which I don't understand... when I try to access the different members of my struct... they don't show up when I press ctrl+space, which suggests that the struct hasn't been passed properly...

pointer->someKindOfVariable doesn't seem to work i.e. when I type pointer-> and press ctrl+space, no suggestions pop up, I've got a feeling the problems are linked.

KillGabio
9th February 2012, 15:15
Like wysota said it`s classic c++ problem. In your class where you define the struct and assign values, add some "getValue/s" methods...like:


QString getTextFromLineEdit_1 (){ return this->ui->lineEdit_1->text ();}

then all you need to do from the other class is call:


thisvariableneedstextfromthelineEdit = objectofyourclass.getTextFromLineEdit_1 ();

norobro
9th February 2012, 18:36
I included a function prototype in the header file for the filemanager.cpp file:

void FileManager::fileopen(struct properties *pointer);
In your header file you should only have:
void fileopen(struct properties *pointer);The extra qualification is "FileManager::"

Atomic_Sheep
10th February 2012, 02:33
Like wysota said it`s classic c++ problem. In your class where you define the struct and assign values, add some "getValue/s" methods...like:


QString getTextFromLineEdit_1 (){ return this->ui->lineEdit_1->text ();}



Doesn't that defeat the purpose of passing a struct by pointer if you have to create functions for each of the members within a struct? And method functions are for accessing certain functionality within classes? How is this related to passing a struct by pointer?


In your header file you should only have:
void fileopen(struct properties *pointer);The extra qualification is "FileManager::"

Thanks, changed that part.

P.S. Now I'm getting an error of:

within my clicked slot function, there is no matching function to call to filemanager::fileopen(otherfile::variable*, otherfile::options*)
candidates are filemanager::fileopen(variable*, options*)

The most frustrating part of programming... you always think you're right until you finally figure out where you went wrong, but it's so hard to know exactly what you are messing up/don't know/understand.

wysota
10th February 2012, 09:34
Drop the "struct" keyword in the method prototype.