C ++:在头文件中声明struct return-type函数

时间:2012-12-12 11:16:18

标签: c++ data-structures header-files

我有一个标题文件“check.h”,用于定义以下struct

#ifndef CHECK_H
#define CHECK_H
#include<string>
struct Test{
    std::string check;

};

#endif  

我有另一个头文件“test.h”,它具有以下函数,返回类型为上面定义的struct Test

#ifndef TEST_H
#define TEST_H
#include<string>
#include "check.h"
Test display(std::string);
#endif  

但即使在此标头文件中包含"check.h",我也会收到unable to resolve identifier错误。我该怎么做才能解决这个问题?

2 个答案:

答案 0 :(得分:2)

只要您没有定义名称为Test的其他内容(例如变量或函数),您的代码就可以了。

如果你有,那么你需要明确指出你是指班级而不是其他东西:

struct Test display(std::string);
^^^^^^

虽然更好的解决方案是避免对不同的东西使用相同的名称。

答案 1 :(得分:0)

您应该返回struct Test,因为test.h定义的结构不是类型。

或者只是将结构定义更改为typedef:

typedef struct s_Test{
    string check;
} Test;

请参阅维基百科there