好的,首先我要说的是,我的英语并不优秀:D
我在VS2013中用C ++,Windows Forms编写项目。
我有3个表格和1个自己的班级。
我不想使用从文本框(String ^)到标准字符串的转换文本,所以我做了一个公共引用类:
public ref class Employee
{
String^ FirstName;
String^ Surname;
String^ IDnumber;
int Payment;
int Age;
bool Sex;
String^ Phone;
String^ Email;
List<String^>^ Regions;
List<String^>^ Skills;
String^ Profession;
public:
Employee(String^ FirstName, String^ Surname, String^ IDnumber, int Payment, int Age, bool Sex, String^ Phone, String^ Email, List<String^>^ Regions, List<String^>^ Skills, String^ Profession);
Employee();
};
现在我正在尝试做这个类的对象的全局通用列表。我不知道怎么做;)
我正在尝试
List<Employee^> list;
MainWindow.cpp中的出现错误:具有静态存储持续时间的变量不能具有ref类类型
我该怎么办?另一种清单类型?以另一种方式上课?
//编辑:
好的,我作为MainWindow课程的成员完成了它:
#include "Employee.h"
#include "EmployeeCreator.h"
#include "OfferCreator.h"
#pragma once
namespace EmploymentAgency {
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::Generic;
public ref class MainWindow : public System::Windows::Forms::Form
{
public:
MainWindow(void)
{
InitializeComponent();
//
//TODO: W tym miejscu dodaj kod konstruktora
//
}
public: List<Employee^> lista;
....
以第二种形式EmployeeCreator我有:
#pragma once
namespace EmploymentAgency {
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 EmploymentAgency;
using namespace System::Collections::Generic;
/// <summary>
/// Podsumowanie informacji o EmployeeCreator
/// </summary>
public ref class EmployeeCreator : public System::Windows::Forms::Form
{
public:
EmployeeCreator(void)
{
InitializeComponent();
//
//TODO: W tym miejscu dodaj kod konstruktora
//
}
public: MainWindow^ main;
我想做(在MainWindow类中):
private: System::Void AddEmployee_Click(System::Object^ sender, System::EventArgs^ e) {
EmployeeCreator ^window = gcnew EmployeeCreator();
window.main = this; //it doesn't work
window->ShowDialog();
}
对于EmployeeCreator类中的VS2013,MainWindow不是一个类型:
&#34;失踪&#34;;&#34;之前&#34; ^&#34; &#34;
如果我包含&#34; MainWindow.h&#34;包括很多文件。
答案 0 :(得分:0)
以下是使用Employee类中的静态员工列表的示例,它还详细说明了引用列表时List<Employee^>^
的使用情况。
public ref class Employee {
static List<Employee^>^ AllEmployees;
void AddToAllEmployees()
{
if (!AllEmployees)
AllEmployees = gcnew List<Employee^>();
AllEmployees->Add(this);
}
public:
System::String^ Firstname;
System::String^ Surname;
Employee(System::String ^firstname, System::String^ surname)
{
Firstname = firstname;
Surname = surname;
AddToAllEmployees();
};
Employee() {
Firstname = "";
Surname = "";
AddToAllEmployees();
}
~Employee() {
if (AllEmployees)
AllEmployees->Remove(this);
};
static List<Employee^>^ GetAllEmployees() { return AllEmployees; }
};
void testEmployeeClass() {
Employee^ johnDoe = gcnew Employee("John", "Doe");
Employee^ olaNordmann = gcnew Employee("Ola", "Nordmann");
Employee^ nn = gcnew Employee();
List<Employee^>^ everybody = Employee::GetAllEmployees();
for each (Employee^ tmp in everybody) {
// Do something _useful_ with the employees
System::Diagnostics::Debug::WriteLine("Employee: " + tmp->Firstname +" " + tmp->Surname);
}
}
我的测试函数显示输出到Visual Studio 2013的调试窗口,但你肯定有一些更明智的事情与列表。此外,我已将Employee类简化为最低限度(并将成员移至公共部分),以使列表处理更加突出。
此致 甚至
编辑:在你问题的后半部分,你指的是一个&#34; MainWindow isn&#39; ta&#34; -error,只是为了让你进入一点点secret:Visual Studio(以及大多数编译器)提供的错误消息通常会在之前引用错误。这是错误与MainWindow.h无关,但是对于错误列出的行前面的某些内容... 不仅要查看错误行,还要查看报告错误前面的行线!