免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 7473 | 回复: 13
打印 上一主题 下一主题

[C++] vector<struct>()的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-05-07 17:08 |只看该作者 |倒序浏览
A.cpp 中定义vector数组


  1. struct Fact_t{
  2.     int a;
  3.     int b;
  4. };
  5. vector <struct Fact_t>g_FactDB(FACTNUM);
复制代码
在B.cpp饮用时必须加上struct Fact_t的完整定义,否则在用到g_MatchFactDB[0].a时就会出现无法解析"a"的错误。
B.cpp
  1. struct Fact_t{
  2.     int a;
  3.     int b;
  4. };
  5. extern vector <struct MatchFactDB_t>g_MatchFactDB;
复制代码
如果是C.app,同样要加上struct Fact_t的完整定义,而且即使把struct Fact_t的定义放在A.h,并且让B.cpp去include,同样会发生此问题。求解。

用的开发环境是Eclipse+MinGW

论坛徽章:
0
2 [报告]
发表于 2013-05-07 17:28 |只看该作者
vector <struct Fact_t>g_FactDB(FACTNUM);
为什么要加 struct 呢?

论坛徽章:
0
3 [报告]
发表于 2013-05-07 17:32 |只看该作者
anpufeng 发表于 2013-05-07 17:28
vector g_FactDB(FACTNUM);
为什么要加 struct 呢?

http://www.cppblog.com/percyph/archive/2009/03/06/75742.html

论坛徽章:
0
4 [报告]
发表于 2013-05-07 17:34 |只看该作者
那上面 不说了

但在c++里很简单,直接
        struct Student
        {
           int a;
        };
于是就定义了结构体类型Student,声明变量时直接Student stu2;

论坛徽章:
0
5 [报告]
发表于 2013-05-07 17:39 |只看该作者
回复 4# anpufeng

问题我的使用方法不是用这个structure定义变量,而是把它用在vector数组中,并要通过vector调用structure的成员。

这样的话必须要求在每个cpp文件中声明这个vector的变量时,都带上structure的完整定义。这导致同一个structure需要定义多次。否则就无法解析

论坛徽章:
0
6 [报告]
发表于 2013-05-07 17:52 |只看该作者
......头文件是做啥的?

论坛徽章:
0
7 [报告]
发表于 2013-05-07 17:56 |只看该作者
不知道你是不是这个意思? 随便写了个.
Test.h

  1. #ifndef TestStruct_Test_h
  2. #define TestStruct_Test_h

  3. #include <vector>

  4. using namespace std;

  5. struct Fact_t{
  6.     int a;
  7.     int b;
  8. };

  9. #endif
复制代码
main.cpp
  1. #include <iostream>
  2. #include <iterator>
  3. #include <stdio.h>
  4. #include "Test.h"

  5. int main(int argc, const char * argv[])
  6. {
  7.     Fact_t a1;
  8.     a1.a = 5;
  9.     a1.b = 6;
  10.    
  11.     Fact_t b1;
  12.     b1.a = 15;
  13.     b1.b = 16;
  14.    
  15.     Fact_t c1;
  16.     c1.a = 25;
  17.     c1.b = 26;
  18.     vector<Fact_t> FactV;
  19.     FactV.push_back(a1);
  20.     FactV.push_back(b1);
  21.     FactV.push_back(c1);
  22.    
  23.     vector<Fact_t>::iterator it;
  24.     for (it = FactV.begin(); it != FactV.end(); ++it) {

  25.         printf("a: %d\n", it->a);
  26.         printf("b: %d\n", it->b);
  27.     }
  28.     // insert code here...
  29.     std::cout << "Hello, World!\n";
  30.     return 0;
  31. }
复制代码
输出
a: 5
b: 6
a: 15
b: 16
a: 25
b: 26

论坛徽章:
0
8 [报告]
发表于 2013-05-07 17:58 |只看该作者
必须要看到Fact_t的定义,才能使用这个结构体
把你写的代码完整的贴出来,才好帮你分析。

论坛徽章:
0
9 [报告]
发表于 2013-05-07 17:59 |只看该作者
本帖最后由 user2003 于 2013-05-07 18:00 编辑
anpufeng 发表于 2013-05-07 17:56
不知道你是不是这个意思? 随便写了个.
Test.hmain.cpp输出
a: 5

如果把你的程序改一下,
vector<Fact_t> FactV =>  vector<Fact_t> FactV(20)

新增
FactV[0].a=5;

你看看加上面这句能编译过吗?

论坛徽章:
0
10 [报告]
发表于 2013-05-07 18:01 |只看该作者
vector 本身就是容器, 你加20是想干啥?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP