2014-10-27 120 views
0

所以,我正在使用dev-C++。编译器工作正常,一个简单的hello世界程序与其他十几个简单的程序一起工作。这是我正在为班级工作的一项工作。该程序将编译但不能运行。其他程序运行

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

#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(); 
    } 
} 
+3

没有运行时错误? – 2014-10-27 21:13:40

+0

定义“从不运行”。如果你手动运行它会怎么样?你有没有看到任何错误?如果是这样,他们是什么? – Adam 2014-10-27 21:14:00

+0

我看到的第一件事是'getNames'定义的参数不同于你声明的(缺少一个'&') – Fezvez 2014-10-27 21:14:47

回答

1

首先您的getNames声明和执行签名不完全相同。

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

'temp ==“出错了什么? – Barry 2014-10-27 21:16:20

+0

没想到这是我的错误,他以为他使用C字符串而不是C++字符串。 – kyflare 2014-10-27 21:18:08

4

程序应该失败联系起来,因为它无法找到的定义:

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

那是因为你缺少你定义&

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

添加在&,它应该编译和运行很好。

+0

I i st st。感谢队友:P – Brayheim 2014-10-27 21:17:27

+0

@Brayheim:你为什么要用前向声明呢?只是洗牌功能。 – Deduplicator 2014-10-27 21:19:13

+0

@Deduplicator是的,这是为了上课,我的教授对函数原型很奇怪。 – Brayheim 2014-10-27 21:22:23