2011-04-04 149 views
1

我已經離開了C++一段時間,它可能只是我愚蠢,但爲什麼這會給我一個錯誤(代碼下面的錯誤)。C++:追加文本

代碼:

// NetworkServer.cpp : main project file. 

#include "stdafx.h" 
#include "Form1.h" 
#include <winsock2.h> 
#include <iostream> 
using namespace std; 
using namespace NetworkServer; 

[STAThreadAttribute] 
int main(array<System::String ^> ^args) 
{ 
// Enabling Windows XP visual effects before any controls are created 
Application::EnableVisualStyles(); 
Application::SetCompatibleTextRenderingDefault(false); 

// Create the main window and run it 
Application::Run(gcnew Form1()); 


    public void setUsers() 
{ 
    string connectedUsers[] = {"John", "Alex", "Phillip", "Steve"}; 
    Form1->txt_connectedClients.AppendText(connectedUsers[1]); 
} 

    return 0; 
} 

錯誤:

1>NetworkServer.cpp(22): error C2143: syntax error : missing ';' before '->' 
    1>NetworkServer.cpp(22): error C2143: syntax error : missing ';' before '->' 
+0

我們需要看看Form1是一個類還是一個實例,你能提供更多的代碼嗎? – 2011-04-04 21:41:28

回答

2

Form1是類型名稱,您需要一個對象。我看不到代碼的上下文,但只要此代碼寫入Form1類的方法中,那麼this->就可以工作。

public ref class Form1 : public System::Windows::Forms::Form 
{ 
    //... 
public: 
    void setUsers() { 
     array<String^>^ connectedUsers = gcnew array<String^> {"John", "Alex", "Phillip", "Steve"}; 
     this->txt_connectedClients->AppendText(connectedUsers[1]); 
    } 
}; 

請注意您正在使用C++/CLI語言進行編程,而不是C++。

+0

我已更新代碼以顯示上下文。 – 2011-04-04 21:46:01

+0

這遠遠不是合法的C++/CLI(或C++)代碼。你不能在另一個內部任意插入一個函數。而且您還沒有收到有關使用需要對象引用的類型名稱的消息。你將不得不打開書。 – 2011-04-04 21:49:37

+0

對,看起來像我,謝謝。 – 2011-04-04 21:52:37

0

要麼txt_connectedClients不存在或不是一個指針。嘗試使用點運算符。