.net &引用;错误:表达式必须具有“类类型”;关于ref struct C++/CLI
我已经搜索了这个网站上的帖子,虽然还有其他几个人有相同的错误信息,但他们似乎不适用于我的问题 我正在visual studio 2013社区版中使用C++/CLI和.NET framework。我在头文件.net &引用;错误:表达式必须具有“类类型”;关于ref struct C++/CLI,.net,struct,c++-cli,.net,Struct,C++ Cli,我已经搜索了这个网站上的帖子,虽然还有其他几个人有相同的错误信息,但他们似乎不适用于我的问题 我正在visual studio 2013社区版中使用C++/CLI和.NET framework。我在头文件Point.h中定义了refstruct,然后在windows窗体的头文件MainForm.h中创建了的实例。(在MainForm.h中没有#include语句用于Point.h,因为它包含在FractalScript.h中,后者包含在MainForm.h #include "FractalS
Point.h
中定义了ref
struct
,然后在windows窗体的头文件MainForm.h
中创建了的实例。(在MainForm.h
中没有#include
语句用于Point.h
,因为它包含在FractalScript.h
中,后者包含在MainForm.h
#include "FractalScript.h"
#using <System.Windows.Forms.dll>
#pragma once
namespace FractalGenerator {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public ref class MainForm : public System::Windows::Forms::Form
{
public:
MainForm(void)
{
InitializeComponent();
}
public:
void FillRectangleFloat(point::Config^ pConf, bool colour, PaintEventArgs^ e)
{
if (colour)
{
SolidBrush^ brush = gcnew SolidBrush(Color::FromArgb(255, 0, 0, 0));
e->Graphics->FillRectangle(brush, pConf.x, pConf.y, pConf.width, pConf.height);
}
else
{
SolidBrush^ brush = gcnew SolidBrush(Color::White);
e->Graphics->FillRectangle(brush, pConf.x, pConf.y, pConf.width, pConf.height);
}
}
point::Config^ pConf{};
//After this is just a few other variable/function declarations,
//the windows form designer auto generated code, and some event handlers
//which are not relevant to the issue
};
}
每当我试图访问结构
的该实例的任何成员变量时,我都会遇到标题中提到的错误。我不会发布我的全部源代码,因为其中一些源代码与此问题完全独立且不相关。但是,如果您想查看我的全部源代码,我可以发布它
Main.cpp
#include "MainForm.h"
int main()
{
FractalGenerator::MainForm m;
m.ShowDialog();
return 0;
}
Point.h
namespace point
{
ref struct Config
{
//float width is the width in pixels of the shape
float width;
//float height is the height in pixels of the shape
float height;
//float x is the x coordinate in pixels of the top-left corner of the shape
float x;
//float y is the y coordinate in pixels of the top-left corner of the shape
float y;
//int argb_a is the alpha channel of the colour of the shape (value between 0 and 255)
int argb_a;
//int argb_r is the red channel of the colour of the shape (value between 0 and 255)
int argb_r;
//int argb_g is the green channel of the colour of the shape (value between 0 and 255)
int argb_g;
//int argb_b is the blue channel of the colour of the shape (value between 0 and 255)
int argb_b;
};
}
MainForm.h
#include "FractalScript.h"
#using <System.Windows.Forms.dll>
#pragma once
namespace FractalGenerator {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public ref class MainForm : public System::Windows::Forms::Form
{
public:
MainForm(void)
{
InitializeComponent();
}
public:
void FillRectangleFloat(point::Config^ pConf, bool colour, PaintEventArgs^ e)
{
if (colour)
{
SolidBrush^ brush = gcnew SolidBrush(Color::FromArgb(255, 0, 0, 0));
e->Graphics->FillRectangle(brush, pConf.x, pConf.y, pConf.width, pConf.height);
}
else
{
SolidBrush^ brush = gcnew SolidBrush(Color::White);
e->Graphics->FillRectangle(brush, pConf.x, pConf.y, pConf.width, pConf.height);
}
}
point::Config^ pConf{};
//After this is just a few other variable/function declarations,
//the windows form designer auto generated code, and some event handlers
//which are not relevant to the issue
};
}
#包括“FractalScript.h”
#使用
#布拉格语一次
名称空间分形生成器{
使用名称空间系统;
使用名称空间System::ComponentModel;
使用名称空间系统::集合;
使用命名空间System::Windows::Forms;
使用名称空间System::Data;
使用名称空间系统::绘图;
public ref类MainForm:public System::Windows::Forms::Forms
{
公众:
主表格(作废)
{
初始化组件();
}
公众:
void FillRectangleFloat(点::配置^pConf、颜色、PaintEventArgs ^e)
{
if(颜色)
{
SolidBrush^brush=gcnewsolidbrush(颜色:FromArgb(255,0,0,0));
e->图形->填充矩形(画笔,pConf.x,pConf.y,pConf.width,pConf.height);
}
其他的
{
SolidBrush^brush=gcnew SolidBrush(颜色:白色);
e->图形->填充矩形(画笔,pConf.x,pConf.y,pConf.width,pConf.height);
}
}
点::配置^pConf{};
//在这之后,只是一些其他变量/函数声明,
//windows窗体设计器自动生成代码和一些事件处理程序
//这与问题无关
};
}
当我试图引用在第38行声明的pConf
的成员时,错误出现在MainForm.h
的第29行和第34行。同样的错误也出现在我尝试为成员赋值的不同位置。(是的,在我调用所示的函数并尝试引用这些值之前,确实会赋值)
这是一个简单的语法错误,Config是引用类型,pConf是对Config对象的引用,因此您必须使用->
取消引用成员。pConf声明上的^hat是提示
<> >不要混淆<代码> REF StRUCT<代码>,它不是一个ref class
或value struct
您可能打算将配置声明为值类型,但它确实符合该模式。但是,不建议值类型的简单字段不应超过4个。较大的字段效率较低,并且复制和传递值到函数或从函数返回的成本较高。值类型的最佳点是能够适应可用的CPU注册表伊斯特尔斯
修正:
这是一个简单的语法错误,Config是引用类型,pConf是对Config对象的引用,因此您必须使用->
取消引用成员。pConf声明上的^hat是提示
<> >不要混淆<代码> REF StRUCT<代码>,它不是一个ref class
或value struct
您可能打算将配置声明为值类型,但它确实符合该模式。但是,不建议值类型的简单字段不应超过4个。较大的字段效率较低,并且复制和传递值到函数或从函数返回的成本较高。值类型的最佳点是能够适应可用的CPU注册表伊斯特尔斯
修正:
e->Graphics->FillRectangle(brush, pConf->x, pConf->y, pConf->width, pConf->height);