2014-10-27 123 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