2012-12-31 46 views
0

可能重複:
C++: Easiest way to access main variable from function?C++我應該做什麼,而不是全局變量?

我需要從我的主要功能讓我的變量 「輸入」,在a.cpp在b.cpp另一個名爲檢查功能。我在Google和這個論壇上看過它,我發現你可以使用extern這個全局變量來做到這一點,但是這也是不好的,我無法找到一個替代方案的答案。我應該如何在不使用全局變量的情況下將變量中的數據傳遞給其他函數?


我如何獲得參數的代碼工作。 (我想在這裏做的是一個控制檯「經理」,我可以通過輸入解決/查看項目歐拉的解決方案,我在40分鐘前開始編碼)

main.cpp

#include <iostream> 
#include <windows.h> 
#include "prob.h" 

using namespace std; 

int check(string x); 

int main() 
{ 
    string input = "empty"; 
    clear(); 

    cout << "Welcome to the Apeture Labs Project Euler Console! (ALPEC)" << endl << endl; 
    cout << "We remind you that ALPEC will never threaten to stab you" << endl; 
    cout << "and, in fact, cannot speak. In the event that ALPEC does speak, " << endl; 
    cout << "we urge you to disregard its advice." << endl << endl; 
    cin >> input; 
    cin.get(); 
    check(input); 

    cout << input << endl; 
    cin.get(); 
    return 0; 
} 

prob.h

#ifndef PROB_H_INCLUDED 
#define PROB_H_INCLUDED 

int main(); 

int clear(); 
int check(); 
int back(); 

int P1(); 
int P2(); 
int P3(); 
int P4(); 

#endif // PROB_H_INCLUDED 

back.cpp

#include <iostream> 
#include <windows.h> 
#include "prob.h" 

using namespace std; 

int clear() 
{ 
    system("@echo off"); 
    system("color 09"); 
    system("cls"); 

    return 0; 
} 

int check(string x) 
{ 
    if(x == "help"); 

    if(x == "empty") 
    { 
     cout << "And.... You didn't enter anything..." << endl << endl; 
    } 

    else 
    { 
     cout << "Do you have any clue what you are doing? " << endl << endl; 
    } 

    return 0; 
} 
+4

函數參數。瞭解更多。 –

+0

通過「這個論壇/ thingy」,你的意思是Stackoverflow?如果是這樣,你看了什麼問題。有這些:http://stackoverflow.com/questions/9668985/c-easiest-way-to-access-main-variable-from-function,http://stackoverflow.com/questions/11783435/how-to- access-variables-defined-and-declared-in-one-function-in-another-function,http://stackoverflow.com/questions/13594515/c-pass-variable-from-function-outside-main-into- function-call-inside-main,http://stackoverflow.com/questions/2139224/how-to-pass-objects-to-functions-in-c。你的似乎是第一個的複製品。 – jogojapan

+0

這是另一個密切相關的一個:http://stackoverflow.com/questions/6371382/getting-access-to-another-functions-variables – jogojapan

回答

3

通過傳遞d ata作爲函數參數。

例如:

int doSomething(int passedVar) 
{ 
} 

int main() 
{ 
    int i = 10; 
    doSomething(i); 

    return 0; 
} 

注意,函數定義可以在不同的cpp文件甚至駐留。 main只需查看函數聲明,鏈接器應正確鏈接函數定義。

通常,將函數聲明添加到頭文件中,並將頭文件包含在main中,同時在另一個cpp文件中提供函數定義。


告訴你的代碼有問題數:

  • 你並不需要在頭文件中聲明main
  • 您的函數聲明和check()的定義不匹配。你的頭文件說它沒有參數,你定義了一個函數定義來接受一個參數。顯然,它們不匹配。現在他們站在一起,它們是兩個完全不同的功能。

由於編譯器看到它,你宣佈一個功能誰是你從來沒有提供定義,你定義在cpp文件的另一個功能。因此聲明的函數(一個沒有參數)從未定義過,因此定義未找到錯誤。

+0

我做的論點,但我在我的頭和我的主要功能有問題...標題給我一個編譯器錯誤的太多參數,在線調用「dosomething」函數的線上main.cpp錯誤沒有定義的。 – Markus

+0

@Aerzee:通過編輯Q來發布代碼。鏈接錯誤是因爲您並未要求鏈接器鏈接所有源文件。編譯錯誤是因爲您可能在函數聲明和定義中存在不匹配。 –

+0

我得到它的工作,但我不認爲我在最佳的方式做到了,但生病在一秒鐘後發佈的代碼。添加代碼 – Markus

0

Andrei Tita絕對正確。如果你在一個模塊中有一個「值」(例如a.cpp中的「main()」),並且你希望在函數中使用該值(例如b.cpp中的「foo()」)將該值作爲函數參數傳遞!

隨着程序變得越來越複雜,您可能會開始使用類(而不是函數)。