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

Chinaunix

標(biāo)題: 類成員函數(shù)找不到undefined reference to [打印本頁]

作者: mgjrr16888    時間: 2013-08-16 10:55
標(biāo)題: 類成員函數(shù)找不到undefined reference to
定義了一個類,有一個函數(shù)時返回一個vector。

------
StaAgent.h
------

using Ssid_t = struct {
   std::string ssid;
};

class StaAgent {
public:
   vector<Ssid_t> scanSsid();
   ...
}

在StaAgent.cpp里定義了此函數(shù)的實現(xiàn)

------
StaAgent.cpp
------


vector<Ssid_t> StaAgent::scanSsid()
{
   ...
}


在另一個文件里調(diào)用這個

#include "StaAgent.h"
...
void StaDialog:n_scanSsidBt_clicked()
{
   auto _sa = new StaAgent();
   auto ssids = _sa->scanSsid();
   ...
}

鏈接的時候顯示undefined reference to StaAgent::scanSsid()

g++ -Wl,-O1 -Wl,-rpath,/opt/Qt5.1.0/5.1.0/gcc_64 -Wl,-rpath,/opt/Qt5.1.0/5.1.0/gcc_64/lib -o StaTest main.o StaAgent.o StaDialog.o moc_StaDialog.o   -L/opt/Qt5.1.0//5.1.0/gcc_64/lib -lQt5Gui -lQt5Widgets -lQt5Core -lGL -lpthread
StaDialog.o: In function `StaDialog:n_scanSsidBt_clicked()':
StaDialog.cpp.text+0x1d2): undefined reference to `StaAgent::scanSsid()'
collect2: error: ld returned 1 exit status



----------------------------------
首先,鏈接文件里能找到StaAgent.o
然后使用nm -C StaAgent.o
在輸出里,找不到StaAgent::scanSsid()這個符號。

作者: foolishx    時間: 2013-08-16 11:10
那個using ssid_t = struct {
這樣的語法能編譯過嗎?沒見過
作者: mgjrr16888    時間: 2013-08-16 11:14
回復(fù) 2# foolishx


    不好意思,這個用了c++11的語法,包括后面的auto。這個程序應(yīng)該沒有這些語法問題,因為我的.o文件都順利編譯過了。
作者: foolishx    時間: 2013-08-16 11:16
真是與時俱進(jìn)了,我又out了..

作者: bruceteen    時間: 2013-08-16 12:23
這個,我猜,應(yīng)該和C++11沒有任何關(guān)系,我猜你忘了重新編譯 StaAgent.o
要不你把所有*.o都刪除,重新編譯一次試試看
作者: weishuo1999    時間: 2013-08-16 12:23
StaAgent.o StaDialog.o  這兩個的順序調(diào)整一下   
作者: mgjrr16888    時間: 2013-08-16 12:39
bruceteen 發(fā)表于 2013-08-16 12:23
這個,我猜,應(yīng)該和C++11沒有任何關(guān)系,我猜你忘了重新編譯 StaAgent.o
要不你把所有*.o都刪除,重新編譯一 ...


不會的,我每次都make clean了。
作者: mgjrr16888    時間: 2013-08-16 13:52
似乎解決了,原來竟然是using的原因。
我試了下把返回至改為內(nèi)置類型就可以:
vector<int> scanSsid();

于是我又把
using Ssid_t = struct {
};

改為
typedef struct {
} Ssid_t;
竟然就可以了。

看來using和typedef還真不一樣.
作者: bruceteen    時間: 2013-08-16 14:51
回復(fù) 8# mgjrr16888
原來 using 屬于 declare 而不是 define 一個類型
因此,對于匿名類型而言,在不同的編譯模塊中,雖然所有成員都一樣,但也屬于不同的類型。

類似的有:
在一個cpp中
struct {} a;
另一個cpp中
extern struct {} a;
int main()
{
   a;
}
是無法編譯鏈接成功的
作者: myworkstation    時間: 2013-08-16 16:30
回復(fù) 9# bruceteen


    你說的是放到兩個cpp文件中吧,樓主的struct得確是在h文件中,這個問題應(yīng)該是編譯器的bug造成的,g++在這特性上有好幾個bug。標(biāo)準(zhǔn)有云“A typedef-name can also be introduced by an alias-declaration. The identifier following the using keyword
becomes a typedef-name and the optional attribute-specifier-seq following the identifier appertains to that typedef-name. It has the same semantics as if it were introduced by the typedef specifier.”。根據(jù)標(biāo)準(zhǔn)的說法不應(yīng)該有這個問題。但是暫時不建議使用這個特性,支持的編譯太少了,不成熟。

各編譯對新標(biāo)準(zhǔn)的支持程度可以在這兒看到:
http://wiki.apache.org/stdcxx/C++0xCompilerSupport
作者: bruceteen    時間: 2013-08-16 16:50
回復(fù) 10# myworkstation
我覺得也未必,(補(bǔ)充一下,緊接著后面還有一句 In particular, it does not define a new type and it shall not appear in the type-id.)
對于 typedef 而言,typedef確實也不是定義
但 typedef struct {} foo; 是由『struct{}定義』和『typedef聲明』 兩部分組成的
而 using foo = struct {}; 則只有 『typedef聲明』
問題就在于 賦值號 后的 struct {} 算不算定義,我覺得不算。

作者: myworkstation    時間: 2013-08-16 17:01
回復(fù) 11# bruceteen


   
不具名類型有名確的定義:
class-specifier:
class-head { member-specificationopt}
A class-specifier whose class-head omits the class-head-name defines an unnamed class.
通過以上說明可以看出struct {} 就已經(jīng)是一個完整的類型定義了。

進(jìn)一步講不具名類型也具體有鏈接性
a named class or an unnamed class defined in a typedef declaration in which the class has
the typedef name for linkage purposes
it is an unnamed class or enumeration member of a class with linkage;
作者: mgjrr16888    時間: 2013-08-17 16:58
估計是編譯器的問題。即使gcc4.8號稱全部支持,可能還沒多少人真正徹底地應(yīng)用過呢。
作者: Aquester    時間: 2013-08-21 22:55
本帖最后由 Aquester 于 2013-08-21 23:09 編輯
foolishx 發(fā)表于 2013-08-16 11:10
那個using ssid_t = struct {
這樣的語法能編譯過嗎?沒見過


最新C++標(biāo)準(zhǔn)支持的語法,類似于typedef(語法格式不同),需要安裝4.7及以上版本編譯器,推薦本好書《深入理解C++11:C++ 11新特性解析與應(yīng)用》:
http://item.jd.com/11252776.html

進(jìn)一步推薦C++之父個人網(wǎng)站上的文章:
http://www.stroustrup.com/C++11FAQ.html




歡迎光臨 Chinaunix (http://72891.cn/) Powered by Discuz! X3.2