c++ - 返回在单独的头文件 : unknown type name 中定义的 Struct

标签 c++ struct tizen

你好,我正在用 C++(具体来说是 Tizen 平台)做一个项目,我是个新手。如何实现一个返回结构的函数,该结构在单独的头文件中定义?我以为您会喜欢下面的代码。

文件名.h

1.  typedef struct StructName {
2.      int hello;
3.      int there;
4.      int friend;
5.      Foo(int num) : hello(99), there(25) {
6.          friend = num;
7.      }
8.  } StructName;
9.
10. virtual StructName FunctionName(void);

第 10 行的返回类型 StructName 上函数的前向减速似乎正确引用了 FileName.h 文件中的结构。

文件名.cpp

1.  #include FileName.h
2.
3.  StructName FunctionName(void) {
4.      int n = 5;
5.      StructName s(n);
6.      return s;
7.  }

我收到的错误是 FileName.cpp 中 第 3 行 上的“未知类型名称‘StructName’”。
但是,第 5 行的 FileName.cpp 中的 StructName 似乎正确引用头文件中的结构。

我一直试图在 Stack Overflow 上阅读类似的问题,但没有一个答案能解决我的问题。

例如,
如果我在函数的返回类型(在两个文件中)和 FileName.cpp 中函数的返回变量 s 前面添加关键字“struct”,我会收到另一个错误消息:

  • “FileName::FunctionName”的外联定义与返回类型中的声明不同


  • 没有从“struct StructName”(又名“FileName::StructName”)到“struct StructName”(又名“StructName”)的可行转换

最佳答案

对我有用的解决方案可能与 Tizen 相关,因为我之前在 Stack Overflow 上没有看到任何关于此解决方案的帖子(而且 Tizen 并不常见)。如果有人能证实或否认那会很棒。

无论如何,我 friend 想出的解决方案是在 FileName.cpp 中指定的函数的返回类型前面添加“FileName::”(在 FileName.h 中不是必需的)完整的解决方案在下面发布解决问题的要点(FileName.cpp 中的第 3 行)。

文件名.h

1.  typedef struct StructName {
2.      int hello;
3.      int there;
4.      int friend;
5.      Foo(int num) : hello(99), there(25) {
6.          friend = num;
7.      }
8.  } StructName;
9.
10. virtual StructName FunctionName(void);

文件名.cpp

1.  #include FileName.h
2.
3.  FileName::StructName FunctionName(void) { // <- Adding 'FileName::' infront of
4.      int n = 5;                            // the return type fixed the problem
5.      StructName s(n);
6.      return s;
7.  }

关于c++ - 返回在单独的头文件 : unknown type name 中定义的 Struct,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27428071/

相关文章:

json - 如何在golang中获取结构的json字段名称?

arrays - 为什么在创建嵌套结构数组时不能使用短数组构造函数语法?

tizen - 由于签名错误,设备上的应用程序安装失败 - tizen studio

c# - C++程序中引用C#库,其他PC不能

c++ - 错误 C2661 : 'CObject::operator new' : no overloaded function takes 4 arguments

c++ - 在路径追踪器中查找屏幕坐标背后的数学

arrays - 结构 : Fast assignment 的 Matlab 数组

c++ - 使用 std::move 实现 "take"方法

javascript - 如何在三星 gear s2 上启用 http 请求/互联网访问

javascript - 在三星 tizen 电视应用程序中实现多任务功能