该程序将编译但不运行。其他程序运行

时间:2014-10-27 21:12:20

标签: c++

所以,我使用的是dev-C ++。编译器工作正常,一个简单的hello world程序与大约十几个其他简单程序一起工作。这是我正在为课堂工作的一项工作。

对我来说,这个编译但它永远不会运行。它有什么问题?

#include <iostream>
#include <vector>
#include <cstdlib>
#include <algorithm>
using namespace std;

void getNames(vector<string> &vectorName, int &last, string temp);

int main() {
    vector<string> names;
    string tmp;
    int last = 0;

    getNames(names, last, tmp);

    for(int j = 0; j < last; j++) {
        cout << names.at(j) << endl;
    }

    system("PAUSE");
    return EXIT_SUCCESS;
}

void getNames(vector<string> vectorName, int &last, string temp) {

    while (true) {
        cout << "Enter a name (quit to stop): ";
        cin >> temp;
    if (temp == "quit") break;
        vectorName.push_back(temp);
        last = vectorName.size();
    }
}

2 个答案:

答案 0 :(得分:4)

程序无法链接,因为找不到以下定义:

void getNames(vector<string> &vectorName, int &last, string temp);

那是因为你错过了定义中的&

void getNames(vector<string> vectorName, int &last, string temp){
                            ^^^^^^^^^^^

添加&,它应该编译并运行得很好。

答案 1 :(得分:1)

首先,您的getNames声明和实施签名不完全相同。

void getNames(vector<string> &vectorName, int &last, string temp){
void getNames(vector<string> vectorName, int &last, string temp){
相关问题