2013-07-25 74 views
0

這段代碼取自http://msdn.microsoft.com/en-us/library/system.console.windowwidth.aspx 我想在gcc編譯器中編譯它。「Console ::」不編譯

#include<cstdio> 
#include<string> 
#include<windows.h> 
#include<iostream> 
using namespace System; 
int main() 
{ 
    int origWidth; 
    int width; 
    int origHeight; 
    int height; 
    String^ m1 = "The current window width is {0}, and the " 
    "current window height is {1}."; 
    String^ m2 = "The new window width is {0}, and the new " 
    "window height is {1}."; 
    String^ m4 = " (Press any key to continue...)"; 

    // 
    // Step 1: Get the current window dimensions. 
    // 
    origWidth = Console::WindowWidth; 
    origHeight = Console::WindowHeight; 
    Console::WriteLine(m1, Console::WindowWidth, Console::WindowHeight); 
    Console::WriteLine(m4); 
    Console::ReadKey(true); 

    // 
    // Step 2: Cut the window to 1/4 its original size. 
    // 
    width = origWidth/2; 
    height = origHeight/2; 
    Console::SetWindowSize(width, height); 
    Console::WriteLine(m2, Console::WindowWidth, Console::WindowHeight); 
    Console::WriteLine(m4); 
    Console::ReadKey(true); 

    // 
    // Step 3: Restore the window to its original size. 
    // 
    Console::SetWindowSize(origWidth, origHeight); 
    Console::WriteLine(m1, Console::WindowWidth, Console::WindowHeight); 
} 

但它顯示錯誤。

的錯誤是

F:\Untitled2.cpp||In function 'int main()':| 
F:\Untitled2.cpp|31|error: 'Console' has not been declared| 
F:\Untitled2.cpp|32|error: 'Console' has not been declared| 
F:\Untitled2.cpp|33|error: 'Console' has not been declared| 
F:\Untitled2.cpp|34|error: 'Console' has not been declared| 
F:\Untitled2.cpp|35|error: 'Console' has not been declared| 
F:\Untitled2.cpp|35|error: 'name' was not declared in this scope| 
F:\Untitled2.cpp|36|error: 'Console' has not been declared| 
||=== Build finished: 7 errors, 0 warnings (0 minutes, 0 seconds) ===| 

我應該需要添加的代碼?

回答

6

該代碼不是C++,它是C++/CLI。使用Microsoft編譯器。

+0

我不能添加任何頭文件或使用一些連接器到我的code :: blocks來編譯代碼.......? – Maruf

+1

不是。它是一種不同的語言,不僅是一個不同的庫(即''String ^'根本不是C++並且將無法編譯)。 – filmor

2

Console是一個.NET類,只能通過C++/CLI訪問。

正則C++不使用.NET框架,並且無法訪問其中的類。

所以你需要決定你寫的是哪種語言。