【问题标题】:print a variable from function to a textbox in windows form (c++/CLI)将函数中的变量打印到 Windows 窗体中的文本框(c++/CLI)
【发布时间】:2013-06-19 01:30:29
【问题描述】:

您好,我有一个通常在控制台下运行的程序,现在我必须将其设为用户界面。

所以我创建了一个新项目(Windows 窗体)并开始从包含控制台程序的程序中添加头文件 .h 和 .cpp。

我需要将标题中的变量打印到 Windows 窗体中的文本框中。这是我的代码:

这是我需要打印的变量含义

test.h

string **meaning**;

关于test.cpp:

void Engine::Conclude(void)
string Name;
**meaning**= assignmeaning(Name)
cout<<"" <<"->"<<**meaning**;

CWorkspace.cpp

#include "stdafx.h"
#include <fstream>
#include <locale.h>
#include <iostream>
#include "WorkSpace.h"
using namespace std;

CWorkspace::CWorkspace()
    {
        m_pBuilder= new Compiler();

    }

void CWorkspace::LoadKnowledge()
{
CPROPNode* Arbol=Builder->SyntacticTree;
   /*This is not relevant*/
fstream file("..\\Text.txt", ios::in);
        if(file.is_open())
        {
            if(Builder->Compiler(file))
            {
                cout<<" ok"<<endl;
           /*Motor is a local variable from CWorkspace*/

    Motor = new InferenceEngine(Builder->SyntacticTree,Builder); 
    if(Motor->ValidateKnowledge())
        {}
}
}

所以我尝试在我的文本框上执行此操作:

#include test.h
#include <string>
#include <msclr\marshal_cppstd.h>
#include <msclr\marshal_cppstd.h>

using namespace std;
namespace mainForm
{ 
  public ref class Res: public System::Windows::Forms::Form
   {
     public:
        CWorkspace* Workspace;
              Res(void)
                  {
                     InitializeComponent();
                  }

/*
  /*
    /*Auto generated code from windows form*/
                                            */
                                             */
private: System::Void Resultado_Load(System::Object^  sender, System::EventArgs^  e) {

string s = Workspace->Motor->meaning;
             String^ hola = gcnew String(s.cstr());
             textBox1->Text= hola;

但不知何故我无法编译,有人知道如何在文本框上写这个变量吗?

这是我编译时遇到的错误:

错误 C2039:'cstr': 不是 'std::basic_string<_elem>

的成员

虽然我不知道这是否是打印变量的正确方法。

【问题讨论】:

  • 这是 C++/CLI,不是 C++。
  • 我们应该凝视我们的水晶球以了解它是如何编译的吗?您没有发布可编译的示例,因此您有责任向我们提供编译器输出。
  • 什么是 WorkspaceMotor,它们是 namespacetype 你应该写:Workspace::Motor::meaning.
  • 抱歉已编辑,Workspace 是一个 .cpp,Motor 包含在 Worskpace 中

标签: windows forms c++-cli


【解决方案1】:

std::string 方法的名称是c_str。您缺少下划线。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    • 1970-01-01
    • 2013-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多