簡體   English   中英

C ++模板類更改靜態成員的值

[英]C++ template class change static member's value

我有以下類-類Matrix<T>代表類型T的對象的矩陣,它是超類baseMAtrix ,其中包含對所有Matrix<T>類型通用的靜態布爾變量parallel_

我想訪問parallel_但似乎我的代碼未鏈接-

class baseMatrix {
protected:
    static bool parallel_;
};

template<class T>
class Matrix : baseMatrix{

public:
    static void setParallel (bool parallel){
        if(parallel != baseMAtrix::parallel_){
            cout << "message" << endl;
        }
        baseMAtrix::parallel_ = parallel;
    }
};

我收到此消息-

`CMakeFiles/ex3.dir/Tester.cpp.o:Tester.cpp:(.rdata$.refptr._ZN10baseMatrix9_parallelE[.refptr._ZN10baseMatrix9_parallelE]+0x0): undefined reference to `baseMatrix::_parallel'
collect2: error: ld returned 1 exit status`

我將Tester.cpp文件稱為setParallel

Matrix<int>::setParallel(true);

這是調用setParallel的正確方法嗎?

這是訪問baseMatrix::_parallel的正確方法嗎?

如果您想保持標題友好,可以將基類更改為模板:

template<class T>
class baseMatrix 
{
protected:
    static bool _parallel;
};

template<class T>
bool baseMatrix<T>::_parallel;

template<class T>
class Matrix : baseMatrix<void>
...

這樣,在cpp文件中就不需要baseMatrix::_parallel

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM