2013-03-05 32 views
0

我想弄明白爲什麼VS2012不能顯示函數multi from functions.cpp @ main.cpp。爲了讓它顯示在main.cpp中,我必須鍵入int multi();在main.cpp中的Visual Studio顯示函數@ main.cpp

這裏的2個截圖,第一屏幕截圖是在int multi(); main.cpp中

第二個是無。代碼在2個案例中運行良好,但是當我想導航到main.cpp文件中的multi函數時,如果我不輸入int multi(); @ main.cpp

是否有人可以解釋我在做什麼錯了?

謝謝。

實施例圖像1

enter image description here

實施例圖像2

enter image description here

這是我的代碼

Main.cpp的

#include "Header.h" 
#include <iostream> 

using namespace std; 
int plus(); 

int main() 
{ 

cout << "Eneter a number you want to multiply" << endl; 
cout << multi() <<endl; 
cout << randomNumber << endl; 

system("pause"); 
return 0; 
} 

Header.h

#ifndef _HEADER_ 
#define _HEADER_ 

#include <iostream> 

int randomNumber = 4; 
int multi(); 

#endif 

functions.cpp

#ifndef _HEADER_ 
#define _HEADER_ 

#include <iostream> 

using namespace std; 

int multi() 
{ 
    int x; 
    cin >> x; 
    return(x=x+x); 
} 
#endif 

回答

0

這是因爲該下拉列表僅顯示您在當前文件中聲明/定義的項目的名稱。這就是預期的行爲,因爲它的目的是在當前文件中導航,並顯示其他文件中的項目名稱會帶你到不同的文件(再加上它會顯示你的頭像iostream,你不是真的感興趣的東西)。

智能感知的其他功能,你表現出 「多」 的功能,如輸入 「::」:

enter image description here

+0

謝謝!如果我理解它是正確的,如果我使用外部文件來聲明/定義我的函數,我仍然需要在將要調用/使用這些函數的文件中進行前向聲明/定義我的函數。正確? – alentor 2013-03-05 04:03:12

+0

不!你只需要在聲明/定義的地方包含標題。頂部的下拉列表不顯示它們並不意味着它們不可用。就像我說的,智能感知的其他功能確實顯示它們。 – user1610015 2013-03-05 05:03:29

+0

謝謝!你幫助我更多地理解編程語言和工具。謝謝。 – alentor 2013-03-05 06:13:04

-1

您可以用頭文件只用了,是這樣做的:

#ifndef _HEADER_H 
#define _HEADER_H 

#include <iostream> 
using namespace std; 

int randomNumber = 4; 

int multi() 
{ 
    int x = 1; 
    cin >> x; 
    return(x=x+x); 
} 

#endif 

,並用這種方式,你不需要聲明函數。 你試圖做的是創建一個'類'文件? 對你更好,它只需點擊項目上的右鍵>添加>類 ,你可以看到它是如何工作的。

-1

在正常情況下,包括警衛,#ifndef _HEADER_, #define _HEADER_, #endif行不屬於cpp文件。你應該從functions.cpp中刪除它們。

它可能不會導致您在小程序中出現問題,但header.h_HEADER_是名稱的不好選擇。你不應該使用下劃線後跟大寫字母。這裏更多信息:What are the rules about using an underscore in a C++ identifier?