问题
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