2012-12-31 119 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()”)将该值作为函数参数传递!

随着程序变得越来越复杂,您可能会开始使用类(而不是函数)。