C++ : Declaring struct return-type function in header file

狂风中的少年 提交于 2021-01-29 05:57:28

问题


I have a header file "check.h" that defines the following struct:

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

};

#endif  

I have another header file "test.h" that has the following function with return type as the struct Test defined above:

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

But even on including "check.h" in this header file I get an unable to resolve identifier error. What do I do to fix this?


回答1:


Your code should be fine as long as you haven't defined something else (such as a variable or function) with the name Test.

If you have, then you need to make it clear that you're referring to the class and not the other thing:

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

although a much better solution would be to avoid using the same name for different things.




回答2:


You should return struct Test because test.h defines a struct not a type.

Or just change your struct definition to a typedef:

typedef struct s_Test{
    string check;
} Test;

See wikipedia there



来源:https://stackoverflow.com/questions/13838531/c-declaring-struct-return-type-function-in-header-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!