Qt汽车共享项目中调用RegData类成员函数出现类型错误:request for member 'setLogin' in 'givedata' which is of non-class type 'RegData()'
Hey there, let's break down what's causing this error and how to fix it quickly!
The Root Cause
Your error message clearly points to the core issue:
C:\Users\david\Documents\Carsharring_files\mainwindow.cpp:165: error: request for member 'setLogin' in 'givedata', which is of non-class type 'RegData()'
In your mainwindow.h, you declared givedata as a member function that returns a RegData object, not as a RegData class member variable. When you try to call givedata.setLogin(), the compiler sees givedata as a function name (not an object), so it can't find the setter methods that belong to the RegData class.
Step-by-Step Fix
1. Correct the Member Declaration in mainwindow.h
Find where you declared givedata in your MainWindow class. You probably have something like this:
class MainWindow : public QMainWindow { // ... other class members ... RegData givedata(); // This is a function declaration, NOT a variable };
Change it to a RegData member variable by removing the parentheses:
class MainWindow : public QMainWindow { // ... other class members ... RegData givedata; // Now this is a valid RegData object instance };
2. Update the Login Validation Logic in mainwindow.cpp
Now that givedata is a proper RegData object, you can safely call its setter methods when login succeeds:
// Inside your login validation loop if (loginCredentialsMatch) { givedata.setLogin(loginline); givedata.setEmail(emailline); givedata.setPassword(passwordline); givedata.setBalance(fetchedUserBalance); // Replace with your actual balance value from the file }
Optional: Add a Getter Method (If Needed)
If you need to access this RegData instance elsewhere in your code (like displaying the balance in the main menu), add a getter function to your MainWindow class:
// In mainwindow.h class MainWindow : public QMainWindow { // ... RegData getRegData() const { return givedata; } // Getter to access the RegData instance };
Why This Works
By changing givedata from a function declaration to a class member variable, you're creating an actual instance of the RegData class inside your MainWindow. This instance has full access to all the setter and getter methods you defined in the RegData class, so the compiler will no longer throw that "non-class type" error.
内容的提问来源于stack exchange,提问作者Nedaxses

