C++ c++;未找到函数参数标识符
我试着写程序来管理商店。它有一些用户、商品和订单。 这是我的用户.h,好的.h文件: 用户.h:C++ c++;未找到函数参数标识符,c++,C++,我试着写程序来管理商店。它有一些用户、商品和订单。 这是我的用户.h,好的.h文件: 用户.h: #ifndef _USER #define _USER #include "Store.h" #include "Good.h" namespace question1 { class User { const Store store; public: User(Store &s) : sto
#ifndef _USER
#define _USER
#include "Store.h"
#include "Good.h"
namespace question1
{
class User
{
const Store store;
public:
User(Store &s) : store ( s )
{
}
};
class AdminUser:User
{
};
class DeliveryUser:User
{
};
class OrderUser:User
{
void registerOrder(Order &o);
};
class ReceptionUser:User
{
void importGood(Good &g);
void increaseGood(Good &g);
};
}
#endif
很好。h:
#ifndef _GOOD
#define _GOOD
#include <string>
#include <vector>
#include "User.h"
namespace question1
{
class Date
{
public:
Date ();
Date ( int mn, int day, int yr); // constructor
void display(); // function to display date
int GetMonth();
void SetMonth(int mn);
~Date();
private:
int month, day, year;
int DaysSoFar();
};
enum Ordertype{Newly_registered, Check, Answered};
class Order
{
int num;
std::string customerName;
Date registered, Check;
Ordertype type;
std::vector<int> codelist, numlist;
public:
Order();
Order(Order& o);
};
class ImportDate
{
Date importDate;
User importer;
int num;
};
class ExportDate
{
Date exportDate;
User exporter;
int num;
Order ex;
};
class Good
{
std::string name;
int code;
Date in;
int innum, AvailableNum;
User importer;
std::vector<ImportDate> importHistory;
std::vector<ExportDate> exportHistory;
public:
Good();
Good(Good &g);
};
int max (int a, int b)
{
if (a>b) return(a) ; else return (b);
}
int min (int a, int b)
{
if (a>b) return(b); else return (a);
}
}
#endif
\ifndef\u很好
#定义好
#包括
#包括
#包括“User.h”
名称空间问题1
{
上课日期
{
公众:
日期();
日期(整数分钟,整数天,整数年);//构造函数
void display();//用于显示日期的函数
int GetMonth();
无效设置月(整数mn);
~Date();
私人:
int月、日、年;
int DaysSoFar();
};
枚举Ordertype{新注册,检查,应答};
阶级秩序
{
int-num;
std::字符串客户名称;
登记日期,核对;
订单类型;
向量代码表,numlist;
公众:
订单();
订单(订单和订单);
};
进口日期
{
进口日期;
用户进口商;
int-num;
};
类别输出日期
{
出口日期;
用户出口商;
int-num;
订单价;
};
很好
{
std::字符串名;
int代码;
日期在;
int innum,可供选择;
用户进口商;
std::载体重要性;
向量输出历史;
公众:
好的();
好(Good&g),;
};
最大整数(整数a,整数b)
{
如果(a>b)返回(a);否则返回(b);
}
最小整数(整数a,整数b)
{
如果(a>b)返回(b);否则返回(a);
}
}
#恩迪夫
但当我编译这两个代码时,我在用户文件中得到了一个错误
语法错误:标识符“顺序”,第28行
语法错误:标识符'Good',第33行
语法错误:标识符'Good',第34行
在函数参数列表中。
我使用VisualStudio2010。并打开空项目
有人能帮我吗?你有一个循环引用。简言之,你会得到这样的结果:
class Good
{
User * fn() {} // What is a User?
};
class User
{
Good * fn() {}
};
C++的行为不像C#,自上而下地读取文件,因此当它遇到对用户的第一个引用时,它还不知道它是什么。即使切换类的顺序,问题也会返回
您可能应该将它们放在同一个文件中,并使用类型转发:
class User;
class Good
{
User * fn() {} // Ah, User is some class. Go on.
};
class User
{
Good * fn() {}
};
或者(正如Agentlien建议的那样):
这里的问题是,编译器第一次达到类或函数的定义时,需要声明它使用的所有类型。例如,具有:
void importGood(Good &g);
在User.h中,表示必须在此函数声明之前声明Good
class Good;
这需要放在使用此类型的任何函数的声明之前,因为否则编译器不知道它是类型的名称,并且会出现编译器错误
通常,也可以简单地#包含定义此类型的文件。但是,此方法有两个缺点:
这要昂贵得多。即使您使用了include-guard,系统仍然必须读取并标记文件,然后才能确定您对其内容不感兴趣
每当你有一个循环依赖项,比如在你的例子中,它都不会有帮助。编译器会简单地开始编译,例如User.cpp。然后它会包含User.h,并且几乎会立即遇到包含“Good.h”
。从那时起,它会输入Good.h。这样,你(又一次)试图#include User.h
,由于include保护,这将在很大程度上被忽略。因此,一旦在Good.h中使用User
,编译器仍然没有看到它的声明
前向声明解决了这个问题,方法是在第一次使用类之前,将类的声明直接放在使用它的文件中
一般来说,您应该更喜欢在头文件中使用转发声明,并尽可能避免包含其他头文件
实际需要直接从标头中包含某些内容的唯一时间是在创建该标头中声明的类型的实例时(例如,将其作为不是指针或引用的成员变量),或者在使用成员时(例如函数或成员变量)这就是为什么将函数定义放在cpp文件中而不是放在头文件中总是一个好主意的原因。在
#include
语句中有一个循环引用。第一个头文件包括另一个头文件,其中包括第一个头文件,依此类推。@Captain notiver我想这就是原因#ifndef和#define语句是for的,但我还是会给出一个例子shot@TaylorFlores头保护防止头文件的内容被编译多次。它不阻止循环#include
语句。包含保护通过忽略第二个包含而有效地防止循环包含。如果之后的代码取决于包含的标题,但是,您会遇到错误。解决方案:声明类型。顺便说一句:_GOOD和_USER不是允许使用的宏,因为它们是保留的。使用这些非常通用的名称,您也很有可能创建冲突。这也不会编译,因为您正在定义functionGood::fn
在类定义中。这样做,返回类型不能是不完整的类型,因此向前声明它并不能解决问题。您还需要将函数定义移动到User
@Agentlien的定义之后。您是对的,我想到了User*
和Good*
,但是t编写User
和Good
。谢谢验证。修改了代码,它们现在都可以编译了。这样好多了。:)
class Good;