2011-09-22 44 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