2011-09-22 66 views
0

我在C++中學習一個類,並且已經給了我的第一個項目。在課堂上,教授只是談論語法,語法等。他從不談論如何使用Visual Studio。他向我們發送了一個沒有任何解釋的頭文件,並希望我們將它用於該項目。我不太清楚如何開始使用這個文件。我試圖創建一個空的Visual C++項目,添加這個文件並運行它,但是對於其中一個,無處不在和兩個紅色的下劃線錯誤,VS說它找不到可執行文件。如果有人能幫助我獲得VS和/或我的項目,我可以負責編寫這個程序(剛剛用Java編寫完全相同的程序)。只用一個頭文件開始一個新項目

這是他發給我們的頭文件。從外觀上看,他有點sl。。

#pragma once 

namespace control2 { 

    using namespace System; 
    using namespace System::IO; // added by Zhang 
    using namespace System::ComponentModel; 
    using namespace System::Collections; 
    using namespace System::Windows::Forms; 
    using namespace System::Data; 
    using namespace System::Drawing; 

    /// <summary> 
    /// Summary for Form1 
    /// 
    /// WARNING: If you change the name of this class, you will need to change the 
    ///   'Resource File Name' property for the managed resource compiler tool 
    ///   associated with all .resx files this class depends on. Otherwise, 
    ///   the designers will not be able to interact properly with localized 
    ///   resources associated with this form. 
    /// </summary> 
    public ref class Form1 : public System::Windows::Forms::Form 
    { 
    public: 
     Form1(void) 
     { 
      InitializeComponent(); 
      // 
      //TODO: Add the constructor code here 
      // 
      // added by Zhang 
      StreamReader ^sr = gcnew StreamReader("control.txt"); 
      this->choice=Int32::Parse(sr->ReadLine()); 
      sr->Close(); 

     } 

    protected: 
     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     ~Form1() 
     { 
      if (components) 
      { 
       delete components; 
      } 
     } 

    private: 
     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     System::ComponentModel::Container ^components; 

     int choice; 

#pragma region Windows Form Designer generated code 
     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     void InitializeComponent(void) 
     { 
      this->SuspendLayout(); 
      // 
      // Form1 
      // 
      this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); 
      this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; 
      this->ClientSize = System::Drawing::Size(292, 266); 
      this->Name = L"Form1"; 
      this->Text = L"CS351"; 
      this->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &Form1::Form1_Paint); 
      this->ResumeLayout(false); 

     } 
#pragma endregion 
    private: System::Void Form1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e) { 
       Graphics ^g = e->Graphics; 
       // g->Clear(BackColor); 
       // g->Clear(Color::Red); 
       for (int y = 0; y < 10; y++) { 
       // pick the shape based on the user's choice 
       switch (choice) 
       { 
        case 1: // draw rectangles 
         g->DrawRectangle(Pens::Black, 10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10); 
         break; 
        case 2: // draw ovals 
         // g->DrawEllipse(Pens::Black, 10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10); 
         g->DrawArc(Pens::Black, 10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10, 0, 360); 
         break; 
        case 3: // fill rectangles 
         g->FillRectangle(Brushes::Red, 10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10); 
         break; 
        case 4: // fill ovals 
         // g->FillEllipse(gcnew SolidBrush(Color::Red), 10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10); 
         g->FillPie(gcnew SolidBrush(Color::Red), 10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10, 0, 360); 
         break; 
        default: // draw lines 
         g->DrawLine(Pens::Black, 10 + i * 10, 60 + i * 20, 60 + i * 20, 10 + i * 10); 
         break; 
       } // end switch 
       } // end for 

       choice=(choice+1)%5; 
      } 
    }; 
} 
+3

此代碼不是C++ ...關閉,但那些流浪的'^'真的很混亂。不知道他是否輸入了&&。 –

+7

我認爲這是C++ - cli,不是嗎? –

+0

該代碼實際上不會生成程序。它只是定義一些東西。你必須提供實際使用這些東西的代碼。例如教授給你提供了藍圖,但你必須用這些計劃建造一所房子。 –

回答

4

Ughh ...編譯...反正...

如果你打開VS,並轉到文件,你會發現新的項目選項。我假設這是一個Windows窗體應用程序。因此,選擇新項目,在語言下選擇C++,然後選擇Windows窗體應用程序。當這一切都設置好後,進入文件 - >全部保存並將其放置在一個目錄中。現在拿着你的教授給你的文件,並把它放在那個目錄下的其他代碼文件中。返回到您的項目,在解決方案資源管理器下,右鍵單擊標題文件,添加,添加現有項目,然後選擇標題。這應該足以讓你開始!

+0

這正是我所需要的。謝謝。呃......的 –

+1

+1 ...... – Valmond