c++ - C++ 中的 "declaration has no storage class or type specifier"

标签 c++

我正在通过本书(第5版)学习C++,今天我在做练习12.6时遇到了一个问题。

代码如下所示,错误是this declaration has no storage class or type specifier。但是程序可以正常运行。错误位于“}”的最后一行。

#include<iostream>
#include<vector>
#include<new>

using namespace std;

vector<int>* func(){
    return new vector<int>();
}

void read_vec(istream &in, vector<int>* vp){
    int i;
    while(in>>i)
        vp->push_back(i);
}

void print_vec(vector<int>* vp){
    for(int i:*vp)
        cout<<i<<" ";
    cout<<endl;
}

int main(){
    auto vec = func();
    cout<<"Enter a sequence of integers"<<endl;
    read_vec(cin,vec);
    print_vec(vec);
    delete vec;
    vec = nullptr;

    system("pause");
}

我已经搜索了消息,但仍然无法找出我的代码错误的地方。

我将不胜感激所有提示。

最佳答案

你需要标准库头<cstdlib>使用 system .

添加

#include <cstdlib>

在文件的顶部。然后,使用 std::system而不仅仅是 system .

std::system("pause");

关于c++ - C++ 中的 "declaration has no storage class or type specifier",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56334621/

相关文章:

c++ - 使用C++和MFC进行动态图绘制

c++ - 无法将参数从 'SIZE_T *' 转换为 'size_t *' - 如何转换?

c++ - isdigit 在输入 £ 和 ¬ 时引发调试断言

c++ - 模板类和嵌套类 C++

c++ - 在磁盘上/从磁盘存储/加载 C++ 对象

c++ - 当父类没有为自己定义宇宙飞船运算符时覆盖宇宙飞船运算符

c++ - 获取一个QWidget占满整个QMainWindow

c++ - 处理孙子控件的 WM_NOTIFY

c++ - 如何在 C++ 中使用 fstream get() 获取 int

c++ - Skype4COM C++ 事件处理