问题描述
我有一个简单的示例类。它有一个数据成员,它是一个指向犰狳矩阵的指针的std :: vector。构造函数采用这样一个向量作为唯一的参数。这里的文件 TClass.cpp
:
I have a simple example class. It has one data member, which is a std::vector of pointers to armadillo matrices. the constructor takes such a vector as the only argument. here's file 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
。这是我做的:
I want to construct a GTest fixture to test member function GetM
. Here is what I have done:
#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'不是一个类型
。我不明白为什么我不能在fixture定义中构造TClass对象?
I compile this with g++ TClassTest.cpp -o tclass -larmadillo
. It tells me that TClassTest.cpp:24: error: ‘M’ is not a type
. I dont' understand why I cannot construct the TClass object in the fixture definition?
推荐答案
对象T不能在声明中初始化的 TClassTest
。你最近写Java吗? ;-)
The object T cannot be initialized in the declaration of class TClassTest
. Have you been writing Java lately? ;-)
要初始化它,你可以这样做:
To initialize it, you can do something like this:
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;
};
这篇关于GTest fixture当构造函数采用参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!