我在下面的代码示例中遇到问题,其中从另一个向量test2DBVector类型Test2访问向量v1,返回非标量错误。似乎我没有写的语法范围为循环正确。如果有人能帮忙。
#include<iostream>
#include<vector>
#include<string>
class Test1
{
public:
string s1;
class Test2{
public:
vector<string> v1;
};
vector<Test2> test2DBVector;
void updateTest2DBVector()
{
s1 = "Hello World";
Test2 t2Obj;
t2Obj.v1.push_back(s1);
test2DBVector.push_back(t2Obj);
}
};//endofClass Test1
int main()
{
Test1 t1Obj;
t1Obj.updateTest2DBVector();
for(vector<Test1::Test2> tempObj:t1Obj.test2DBVector)
{
for(vector<string> tempString:tempObj.v1)
{
cout<<tempString;
}
}
return 0;
}//endOfMain
Test1 t1Obj;
t1Obj.updateTest2DBVector();
/*
test2DBVector returns the test2 type, so that's why used Test1::Test2 in
for range loop which will define the type of obj, which is test2 type.
obj.v1 returns the string type, thats why used string type in for range
loop to receive that.
*/
for(Test1::Test2 obj:t1Obj.test2DBVector)
{
for(string s:obj.v1)
{
cout<<s;
}
}