下面的代碼編譯。使用typedef for unique_ptr模板
模板之前matrix.h
template<typename T>
class Matrix
{
public:
//...
unique_ptr<Matrix<T>> Test() const;
};
模板
template<typename T>
unique_ptr<Matrix<T>> Matrix<T>::Test() const
{
unique_ptr<Matrix<T>> a{ new Matrix<T>{ 1, 1 } };
return std::move(a);
}
之前matrix.cpp我想用一個typedef(使用)縮短的事情,因爲我認爲這會是更具可讀性,但我的更改會導致錯誤。這是相關的變化。
模板後matrix.h
template<typename T>
class Matrix
{
public:
//...
MatrixUniq<T> Test() const;
};
template<class T> using MatrixUniq = unique_ptr<Matrix<T>>;
後matrix.cpp模板
template<typename T>
MatrixUniq<T> Matrix<T>::Test() const
{
MatrixUniq<T> a{ new Matrix<T>{ 1, 1 } };
return std::move(a);
}
編譯這些更改後崩潰,VC++編譯器的兩倍,但也產生了一些錯誤:
Error C2143 syntax error: missing ';' before '<'
Error C4430 missing type specifier - int assumed.
Error C2238 unexpected token(s) preceding ';'
Error C1903 unable to recover from previous error(s);
我的typedef實現有什麼問題?謝謝。
編輯: 我正在使用VS2015。我正在建立一個靜態庫。在matrix.cpp的底部我:
template class VMatrix<double>;
我認爲你是對的。有沒有更短的做法,而不是冗長的矩陣 :: MatrixUniq 。最好是其他類可以在其他類中看到它的某種方式,我可以說:'MatrixUniq m = Foo();'? –
@PhloxMidas請參閱我添加的關於直接在全局名稱空間中進行的編輯。 – emlai
直接編譯。非常感謝你。 –