错误: 无效使用非静态数据成员"应用程序::应用程序构造函数"

error: invalid use of non-static data member 'Application::ApplicationConstructor'

本文关键字:应用程序 数据成员 构造函数 静态 无效 错误      更新时间:2023-10-16

>我有这个:

#ifndef APPLICATION_H
#define APPLICATION_H
#include "UserOpinion.h"
#include "ApplicationConstructor.h"
#include <vector>
using namespace std;
class Application{  
public:
Application(char *, string, string, ApplicationConstructor &, float); //Application's Constructor
Application(const Application &); //Copy Constructor
virtual void ShowData() const = 0; //Virtual ShowData() Method)
virtual ~Application(); //Destructor  
bool operator== (const Application &) const; //Overload the == operator, this case overloads and the case of != operator
vector<ApplicationConstructor> getApplicationConstructorVector(); //Get the Application Constructor Vector
vector<UserOpinion> getUserOpinionVector(); //Get the User Opinion Vector       
void setApplicationConstructorVector(vector<ApplicationConstructor> &); //Set the Application Constructor Vector
void setUserOpinionVector(vector<UserOpinion> &); //Set the User Opinion Vector
protected:
char *ApplicationCode;
string ApplicationName;
string ApplicationVersion; 
float Price;
ApplicationConstructor &ApplicationConstructor;
UserOpinion *UserView;
vector<ApplicationConstructor> &ApplicationConstructorVector; // Vector with Application Constructor Objects
vector<UserOpinion> &UserOpinionVector; // Vector pointer to User Opinion Objects
};
#endif /* APPLICATION_H */

而这个:

#ifndef APPLICATIONCONSTRUCTOR_H
#define APPLICATIONCONSTRUCTOR_H
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
class ApplicationConstructor{       
private:
string ConstructorCode;  //Constructor's code
char  *ConstructorName;  //Constructor's name
string ConstructorEmail; //Constructor's email
public:
ApplicationConstructor(string, char *, string);  //Constructor
ApplicationConstructor(const ApplicationConstructor&);  //Copy constructor
void setConstructorCode(string);  //Set the constructor's code
void setConstructorName(char *);  //Set the constructor's name
void setConstructorEmail(string);  //Set the constructor's email
string getConstructorCode();  //Get the constructor's code
string getConstructorEmail();  //Get the constructor's email
char *getConstructorName();  //Get the constructor's name
bool operator== (const ApplicationConstructor &) const; //Overloading the == operator for the ApplicationConstructor class
bool operator= (const ApplicationConstructor &); //Overloading the = operator for the ApplicationConstructor class
void showData(); //Show the Application Constructor Data Method
virtual ~ApplicationConstructor();  //Destructor 
};
#endif /* APPLICATIONCONSTRUCTOR_H */

我得到以下信息:

In file included from Application.cpp:8:0:
Application.h:55:15: error: invalid use of non-static data member 'Application::ApplicationConstructor'
vector<ApplicationConstructor> &ApplicationConstructorVector; // Vector with Application Constructor Objects
^~~~~~~~~~~~~~~~~~~~~~
Application.h:53:32: note: declared here
ApplicationConstructor &ApplicationConstructor;
^~~~~~~~~~~~~~~~~~~~~~
Application.h:55:15: error: invalid use of non-static data member 'Application::ApplicationConstructor'
vector<ApplicationConstructor> &ApplicationConstructorVector; // Vector with Application Constructor Objects
^~~~~~~~~~~~~~~~~~~~~~
Application.h:53:32: note: declared here
ApplicationConstructor &ApplicationConstructor;
^~~~~~~~~~~~~~~~~~~~~~
Application.h:55:15: error: invalid use of non-static data member 'Application::ApplicationConstructor'
vector<ApplicationConstructor> &ApplicationConstructorVector; // Vector with Application Constructor Objects
^~~~~~~~~~~~~~~~~~~~~~
Application.h:53:32: note: declared here
ApplicationConstructor &ApplicationConstructor;
//Constructor
Application::Application(char *applicationCode, string applicationName, 
string applicationVersion, ApplicationConstructor &appConstructor ,
float price ):
ApplicationConstructor(appConstructor),
ApplicationConstructorVector(*(new vector<ApplicationConstructor>())),
UserOpinionVector(*(new vector<UserOpinion>())){
int i = strlen(applicationCode);
ApplicationCode = new char[i+1];
strncpy(ApplicationCode, applicationCode, (i+1));
ApplicationName = applicationName;
ApplicationVersion = applicationVersion;
Price = price;
UserView = nullptr;
if (!(this->FindApplicationConstructorOnVector(ApplicationConstructor))){
this->AddToApplicationConstructorVector(this->ApplicationConstructor);
}
}

有什么建议吗?该程序运行良好。.我认为 const 值正在发生一些事情,它将 Application.h 的第一行和 at 向量标记为红色。请提出任何建议?Cpp 文件只有运行良好的构造函数,但我不知道发生了什么......

没有看到 cpp 很难分辨。不过,这一行是可疑的:

vector<ApplicationConstructor> &ApplicationConstructorVector; 

&使它成为引用,这意味着它需要是对某些内容的引用,并且不能默认初始化。如果这不能解决您的问题,可以分享更多代码。

ApplicationConstructor &ApplicationConstructor;

不要将变量和函数命名为与类型相同的名称。这会混淆编译器。