VoydAngel
VoydAngel

Reputation: 36

C++/CLI Console::WriteLine with Vector of User Defined Objects

I have a vector of User Defined Objects and I want to simply output some of the class members (fields?) to the console, but when I try, the program crashes. I have tried to display the 'EmployeeID' directly (it's an Int), as well as converting it to a System::String, and a std::string, using several methods for each (marshalling, converting to char array, etc).

std::vector<Employee> employee;
System::Console::WriteLine("Employee ID: " + employee.at(i).getEmployeeId());

I am at a loss as to why I can not simply print to the console, but I'm quite new to CLI, so maybe it's an easy fix and a simple mistake? I have the same issue with other variables stored in the employee object, those other items are std::string type.

I would prefer to use System::String instead of std::string for those other members, but when I do that I get a weird error message ("A member of a non-managed class cannot have a ref class type or interface class type"). Any help with either issue would be great, but this post is specifically for the Console::WriteLine problem.

Upvotes: 0

Views: 336

Answers (1)

VoydAngel
VoydAngel

Reputation: 36

as per @Ben Voight's suggestions:

Changing my class to a "ref" class and all of my std::string items to System::String^ items has worked.

(It did cause other issues with my code, but that's a different problem and is not related to the the fact that his answer/solution fixed this specific question/problem.)

Upvotes: 0

Related Questions