我是visual C++的新手。我写了我的课,但它有错误
错误1错误C3845:"CRegistration::list1":只有静态数据成员才能在ref类或值类型c:\users\marco\datadesk\cs351\hw3\project3\CRegistration.h 44 1 project3 内初始化
我知道编译器已经说明了错误是什么。但是,我不明白这意味着什么。以下是我的代码
#pragma once
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Collections;
using namespace System::Collections::Generic;
public ref class CRegistration
{
public:
CRegistration(void){
ClickBoxRecord = 0;
ClickBoxRecord = 0;
};//end constructor
void SetRadioRecord(int flag){ RadioRecordRecord = flag; }
void SetClickBoxRecord(int flag){ ClickBoxRecord = flag; }
int GetRadioRecordRecord(){return RadioRecordRecord}
int GetClickBoxRecord(){return ClickBoxRecord}
protected:
private:
int RadioRecordRecord;
int ClickBoxRecord;
LinkedList< int > list1 = gcnew LinkedList< int >();
LinkedList< String^ > ^list2 = gcnew LinkedList< String^ >();
LinkedList< String^ > ^list3 = gcnew LinkedList< String^ >();
LinkedList< String^ > ^list4 = gcnew LinkedList< String^ >();
LinkedList< String^ > ^list5 = gcnew LinkedList< String^ >();
};//end ref class
您是想学习C++还是想学习CLI?
// native C++
#include <list>
#include <string>
std::list<int> list1;
std::list<double> list2;
std::list<std::string> list3;
std::list<std::string> list4;
std::list<std::string> list5;
p.S.
问题是非静态成员list1到list5的内联初始化。在C++中,非静态成员的初始化通常在初始化器列表或构造函数的主体中完成。在C++中,我们通常尽可能多地使用堆栈,只有在必要时才使用堆,因为内存管理很重要。
// C++/CLI
// you can either make a list of each
LinkedList< int > list1; // list of int
LinkedList< double > list2; // list of double
LinkedList< String > list3; // list of String
// or make an object that has one of each
public ref class AllThree {
public:
// members
String str;
double dbl;
int i;
};
// and make a list of that object
LinkedList<AllThree> list4; // list of AllThree