我有一个简单的示例类。它有一个数据成员,它是指向犰狳矩阵的std :: vector指针。构造函数将这样的向量作为唯一参数。这是文件TClass.cpp
:
#include <armadillo>
#include <vector>
class TClass {
private:
std::vector<arma::mat * > mats;
public:
TClass(std::vector<arma::mat * > m_);
arma::mat * GetM( int which ){ return( mats.at(which) );};
};
TClass::TClass(std::vector<arma::mat * > m_){
mats = m_;
}
我想构建一个GTest fixture来测试成员函数GetM
。这就是我所做的:
#include <gtest/gtest.h>
#include "TClass.cpp"
class TClassTest : public ::testing::Test {
protected:
int n;
int m;
std::vector<arma::mat * > M;
virtual void SetUp() {
n = 3;
m = 2;
arma::mat M1 = arma::randu<arma::mat>(n,m);
arma::mat M2 = arma::randu<arma::mat>(n,m);
M.push_back( &M1);
M.push_back( &M2);
}
// virtual void TearDown() {}
// initiate a TClass object
TClass T(M);
};
// my test
TEST_F(TClassTest, CanGetM1){
EXPECT_EQ( T.GetM(0), M.at(0) );
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
我用g++ TClassTest.cpp -o tclass -larmadillo
编译它。它告诉我TClassTest.cpp:24: error: ‘M’ is not a type
。我不明白为什么我不能在夹具定义中构造TClass对象?
答案 0 :(得分:4)
无法在类TClassTest
的声明中初始化对象T.你最近在写Java吗? ;-)
要初始化它,您可以执行以下操作:
class TClassTest : public ::testing::Test {
// ... (rest of code is fine as is)
virtual void SetUp() {
// ...
T = new TClass(M);
}
virtual void TearDown() { delete T; }
TClass *T;
};