使用 Visual C++字符串变量作为对象

发布于 2024-11-03 09:42:51 字数 532 浏览 4 评论 0原文

我刚刚学习 C++,在经历了从使用 HTML、CSS 和 Javascript 到一种我仍然不太理解的全新代码编写方式的震惊之后,我正在慢慢掌握它的窍门。但我已经能够制作一个网络浏览器程序。

现在我一直需要使用变量,并且在一些谷歌搜索之后找到了如何使用字符串(或者至少是一种让它们为我工作的方法),如下所示:

#include <string>
namespace Browser1 {
      using namespace std;
...
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
      String^ var = "label1";
      String^ var2 = "hello world";
      var->Text = var2;
}

但是后来我收到这些错误,说 Text 是一个不明确的符号。我几乎可以肯定这是因为我使用了变量作为对象,但为什么呢?

I am just learning C++ and slowly getting the hang of it after such a shock from working with HTML, CSS and Javascript to a completely new way of writing code which I still don't really understand. But I have been able to make a web browser program.

Now I've been needing the use of variables, and after some Googling have worked out how to use strings (or at least a way to get them working for me) like so:

#include <string>
namespace Browser1 {
      using namespace std;
...
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
      String^ var = "label1";
      String^ var2 = "hello world";
      var->Text = var2;
}

But then I get these errors saying that the Text is an ambiguous symbol. I am almost certain this is because I have used the variable as the object but why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空宴 2024-11-10 09:42:52

您认为哪个对象被类型化为 Object 导致了这种情况?

你的问题是

      var->Text = var2;

哪个是垃圾,因为 var 被声明为 String^ (两行),所以它不能有 Text 属性。当然,您打算使用另一个变量? (比如发件人?)

Which object would you think is typed as Object that causes this?

Afaict your problem is

      var->Text = var2;

which is rubbish, because var is declared as String^ (2 lines up) so it can't have the Text property. Surely, you were intending to use another variable? (Such as sender?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文