我已经离开 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 '->'
最佳答案
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++。
关于C++ : Appending text in form,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5544625/