C++ 具有向量成员的全局结构
为什么这个简单的代码块没有编译C++ 具有向量成员的全局结构,c++,vector,struct,C++,Vector,Struct,为什么这个简单的代码块没有编译 //using namespace std; struct test { std::vector<int> vec; }; test mytest; void foo { mytest.vec.push_back(3); } int main(int argc, char** argv) { cout << "Vector Element" << mytest.vec[0] << endl;
//using namespace std;
struct test {
std::vector<int> vec;
};
test mytest;
void foo {
mytest.vec.push_back(3);
}
int main(int argc, char** argv) {
cout << "Vector Element" << mytest.vec[0] << endl;
return 0;
}
谢谢,
Mustafa您需要包括向量头文件
#include <vector>
#include <iostream>
struct test {
std::vector<int> vec;
};
test mytest;
void foo() {
mytest.vec.push_back(3);
}
int main(int argc, char** argv)
{
foo();
if (!mytest.vec.empty()) // it's always good to test container is empty or not
{
std::cout << "Vector Element" << mytest.vec[0] << std::endl;
}
return 0;
}
#包括
#包括
结构测试{
std::vec;
};
测试我的测试;
void foo(){
mytest.vec.推回(3);
}
int main(int argc,字符**argv)
{
foo();
if(!mytest.vec.empty())//测试容器是否为空总是好的
{
std::cout记住包含适当的文件:
#include <vector>
#包括
您缺少
标题
#include <vector>
#包括
如果代码示例已完成,则未包含向量头或iostream头。此外,在没有()参数的情况下,错误地声明了foo函数:
#include <vector>
#include <iostream>
using namespace std;
struct test {
std::vector<int> vec;
};
test mytest;
void foo() {
mytest.vec.push_back(3);
}
int main(int argc, char** argv) {
cout << "Vector Element" << mytest.vec[0] << endl;
return 0;
}
#包括
#包括
使用名称空间std;
结构测试{
std::vec;
};
测试我的测试;
void foo(){
mytest.vec.推回(3);
}
int main(int argc,字符**argv){
你能记得把
#包括在内吗?
#include <vector>
#include <iostream>
using namespace std;
struct test {
std::vector<int> vec;
};
test mytest;
void foo() {
mytest.vec.push_back(3);
}
int main(int argc, char** argv) {
cout << "Vector Element" << mytest.vec[0] << endl;
return 0;
}