2011-04-04 80 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不存在或不是一个指针。尝试使用点运算符。