1. 程式人生 > >類中靜態變數未定義導致undefined reference to static class member問題的解決方法

類中靜態變數未定義導致undefined reference to static class member問題的解決方法

undefined reference to ***這個連結錯誤的花樣總是層出不窮(more),這一次是找不到類中的成員。

例子1:undefined reference to VS. 類靜態成員變數

在檔案A.h中聲明瞭類A與類B:
class A

{

friend class B;

static int pa;                  // 注:這裡把成員變數宣告為static

}

class B

{
     public:
         void funB();//funB()用到 A::pa;
}


問題:
gcc返回連結錯誤:undefined reference to 'A::pa'。然而,把類B中的pa宣告為非static變數,則可以通過編譯。


WHY:
先複習一下static data members in class。
[1] a single piece of storage for a static data member, regardless of how many objects of that class you create.
[2] the static data belongs to the class. Its name is scoped inside the class and all objects share that data member.
[3] NOTE: The linker will report an error 
if a static data member is declared but not defined.

所以,問題就出在:在class中,無法對static data member進行復制,即便是在建構函式中對static data member進行賦值,linker還是會報錯。
因為static data member不屬於任何一個物件,所以即便是在建立物件的時候進行賦值,也只能說明,這個物件對這個data member重新賦值而已。
因此,這裡的undefined reference to ***,是找不到類靜態成員變數的定義。

解決方法:
The definition must occur outside the class (no inlining is allowed), and only one definition is allowed.

It is common to put it in the implementation file for the class.
其實,也就是在全域性的地方對靜態成員變數賦值。

在上面的例子中,可在檔案A.cpp中在其他函式定義之外,包括在建構函式之外,加上pa的定義,此時不需要加static字首,但要加類名限定,也可以不賦值。

A* B::pa;

相關推薦

靜態變數定義導致undefined reference to static class member問題的解決方法

undefined reference to ***這個連結錯誤的花樣總是層出不窮(more),這一次是找不到類中的成員。例子1:undefined reference to VS. 類靜態成員變數在檔案A.h中聲明瞭類A與類B:class A{ friend class

PHP 編譯安裝時出現 undefined reference to `libiconv' 錯誤的解決方法

原文:http://flyfishes.blog.51cto.com/3527694/819693 手動編譯PHP安裝時遇到如下錯誤 /usr/local/src/php-5.3.10/ext/xmlrpc/libxmlrpc/encodings.c:73: und

6. "undefined reference to" 問題彙總及解決方法 ------非常非常好的一篇文章

轉載地址: https://segmentfault.com/a/1190000006049907?utm_source=tuicool&utm_medium=referral在實際編譯程式碼的過程中,我們經常會遇到"undefined reference to"的問

"undefined reference to" 問題彙總及解決方法 ------非常非常好的一篇文章

         轉載地址: https://segmentfault.com/a/1190000006049907?utm_source=tuicool&utm_medium=referral 在實際編譯程式碼的過程中,我們經常會遇到"undefined r

靜態變數

        類標頭檔案中聲名的靜態變數,不管是哪種都只是作為聲名,而不是定義,比如說有一個類叫Student,在另外一個類A裡面聲名了一個靜態的Student成員,static Student student,那麼這個student還不能用,因為他只是

Spring給靜態變數賦值

spring-context.xml中程式碼 <bean class="com.apricotforest.doctor.pocket.solrsearch.util.SolrUtils" i

CodeBlocks連結時報找到錯誤 undefined reference to

編寫帶有標頭檔案的類和其實現分別放在不同的檔案中,在VC++中正常編譯連結執行, 而在CodeBlocks中出現如下錯誤: ||=== xz, Debug ===| obj\Debug\main.o||In function main':| F:\demo\CodeBlock

android開發時建立xml檔案後在通過R.layout找不到相應的xml檔案的解決方法

今天在學習android開發時遇到了一個很頭疼的問題,明明已經建立了xml檔案,卻在類中通過R.layout找不到相應的檔案,後來才發現在寫程式碼時通過快捷鍵alt+/匯入R.layout檔案時匯入了系統的R檔案而不是自己專案的R檔案,所以在類中通過R.layout找不到相

Linux下undefined reference to pthread create 問題解決

                接觸了Linux系統程式設計中的執行緒程式設計模組,可gcc sample.c(習慣把書上的sample程式碼寫進sample.c檔案中)出現“undefined reference to ‘pthread_create’”,所有關於執行緒的函式都會有此錯誤,導致無法編譯通過。

GCC編譯uboot出現(.ARM.exidx+0x0): undefined reference to `__aeabi_unwind_cpp_pr0'錯誤的解決辦法

/opt/arm-2010.09/bin/../lib/gcc/arm-none-linux-gnueabi/4.5.1/armv4t/libgcc.a(_bswapsi2.o):(.ARM.exid

類似undefined reference to `vtable for `問題解決

問題現象 今天編寫程式碼,編譯的時候遇到了類似“undefined reference to vtable for <classname>” 的問題 ,網上查了好久都說是基類的解構函式沒有

Eclipse在JDK原始碼設定斷點進行除錯報錯Unable to install breakpoint的解決方法

錯誤原因是匯入了錯誤的JRE System Library,即匯入了錯誤的JDK庫。 第一步:依次點選Window->Preferences->Java->Installed JREs,編輯選中的JRE,選擇目錄為JDK的安裝目錄:(這裡應該選擇的目錄是J

thread_join.c:(.text+0x6f): undefined reference to `pthread_create' 問題的解決

今天在測試一個執行緒例子時,結果出現如下錯誤 [[email protected] fzf]# gcc thread_join.c -o thread_join /tmp/cc1HozRv.o: In function `main': thread_join.

C#學習筆記之六 定義static關鍵字的用法暨C#靜態變數的意義與使用

<span style="font-size:18px;">using System; namespace MakeoutStatic { class Counter { public static int num; public void clear() { num = 0;

【c++】為什麼靜態(static)成員不能在定義內初始化?

先看一段程式碼: #include <iostream> using std::cout; //class class loopsaker { public: static int a=1; // 錯誤! loopsaker(); }; int ma

【小家java】靜態程式碼塊、構造程式碼塊、靜態變數執行順序和繼承邏輯

相關閱讀 每篇一句 上帝給每個人都安排了幸福的一生,我們的任務就是把它走完 1、概述 誠如各位所知,java的三大特性:封裝、繼承、多型。其中繼承,是java中最有學問的一點也是最相對來說最難理解的一些東西,本文針對於此,做一些例項分析,希望能夠幫助大家

靜態方法靜態變數靜態程式碼塊,構造方法,以及被@PostConstruct修飾的方法執行順序

第一種:當直接呼叫(沒有new物件)靜態方法時候:程式碼塊(靜態變數按照順序)就執行—–方法執行。 第二種建立物件:執行父類靜態程式碼—-執行子類的靜態程式碼—-執行父類構造方法—–執行子類的構造

java靜態靜態變數靜態方法分別有什麼特點?

一、static  請先看下面這段程式:public class Hello{ public static void main(String[] args){ //(1) System.out.println("Hello,world!"); //(2

java區域性變數,例項變數,/靜態變數區別解析

區域性變數: 區域性變數的方法,建構函式或塊中宣告。 建立區域性變數的方法,建構函式或塊時進入,一旦退出方法,建構函式或塊中的變數將被銷燬。 訪問修飾符不能用於區域性變數。 區域性變數是可見的

預定義的型“System.Object”定義導入

sys 方案 解決方案 csp 重新 未定義 有用 解決 運行 打開一個以前的程序 ,發現報這個錯誤。檢查了程序,發現程序的引用 System 不見了 ,嘗試 引用失敗。。 查了有人說重新建立 Sln文件有用。。 一頭霧水,隨後 嘗試操作 ,程序有用了 具體步驟: 1 找到