亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
12下一頁
最近訪問板塊 發(fā)新帖
查看: 7746 | 回復: 13
打印 上一主題 下一主題

[C++] vector<struct>()的問題 [復制鏈接]

論壇徽章:
0
跳轉到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 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,同樣會發(fā)生此問題。求解。

用的開發(fā)環(huán)境是Eclipse+MinGW

論壇徽章:
0
2 [報告]
發(fā)表于 2013-05-07 17:28 |只看該作者
vector <struct Fact_t>g_FactDB(FACTNUM);
為什么要加 struct 呢?

論壇徽章:
0
3 [報告]
發(fā)表于 2013-05-07 17:32 |只看該作者
anpufeng 發(fā)表于 2013-05-07 17:28
vector g_FactDB(FACTNUM);
為什么要加 struct 呢?

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

論壇徽章:
0
4 [報告]
發(fā)表于 2013-05-07 17:34 |只看該作者
那上面 不說了

但在c++里很簡單,直接
        struct Student
        {
           int a;
        };
于是就定義了結構體類型Student,聲明變量時直接Student stu2;

論壇徽章:
0
5 [報告]
發(fā)表于 2013-05-07 17:39 |只看該作者
回復 4# anpufeng

問題我的使用方法不是用這個structure定義變量,而是把它用在vector數組中,并要通過vector調用structure的成員。

這樣的話必須要求在每個cpp文件中聲明這個vector的變量時,都帶上structure的完整定義。這導致同一個structure需要定義多次。否則就無法解析

論壇徽章:
0
6 [報告]
發(fā)表于 2013-05-07 17:52 |只看該作者
......頭文件是做啥的?

論壇徽章:
0
7 [報告]
發(fā)表于 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 [報告]
發(fā)表于 2013-05-07 17:58 |只看該作者
必須要看到Fact_t的定義,才能使用這個結構體
把你寫的代碼完整的貼出來,才好幫你分析。

論壇徽章:
0
9 [報告]
發(fā)表于 2013-05-07 17:59 |只看該作者
本帖最后由 user2003 于 2013-05-07 18:00 編輯
anpufeng 發(fā)表于 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 [報告]
發(fā)表于 2013-05-07 18:01 |只看該作者
vector 本身就是容器, 你加20是想干啥?
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術有限公司. 版權所有 京ICP備16024965號-6 北京市公安局海淀分局網監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯網協會會員  聯系我們:huangweiwei@itpub.net
感謝所有關心和支持過ChinaUnix的朋友們 轉載本站內容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP