使用 Visual C++字符串变量作为对象
我刚刚学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您认为哪个对象被类型化为 Object 导致了这种情况?
你的问题是
哪个是垃圾,因为
var
被声明为String^
(两行),所以它不能有 Text 属性。当然,您打算使用另一个变量? (比如发件人?)Which object would you think is typed as Object that causes this?
Afaict your problem is
which is rubbish, because
var
is declared asString^
(2 lines up) so it can't have the Text property. Surely, you were intending to use another variable? (Such as sender?)