I’m writing a Windows Form Ap that manages a series of website. I created a website class then pre-loaded std::list<WebSite> from an INI file. Now I need to use that list in other places in the code so I tried to move it into the private member area and got the following error message:

error C4368: cannot define 'websites2' as a member of managed 'HowToInit::Form1': mixed types are not supported

Here is the code the class and also Form1.h. The commented out line 55 “private: std::list<WebSite> websitesX;” is what causes the error.

Lines 131 to end

private: System::Void Form1_Load(...) method is where I initialize the list currently.

#pragma once
#include <string>

class WebSite
{
public:
	WebSite(void);
	WebSite(std::string Name, std::string Path);
	bool operator < (WebSite);
public:
	std::string name;
	std::string path;
};
#include "StdAfx.h"
#include "WebSite.h"

WebSite::WebSite(void)
{
}
WebSite::WebSite(std::string domainName, std::string localPath)
	{
		name = domainName;
		path = localPath;
	}

bool WebSite::operator < (WebSite param) {
	unsigned int i=0;
	while ( ( i < name.length() ) && ( i < param.name.length() ) )
	{
		if (tolower(name[i])<tolower(param.name[i])) return true;
		else if (tolower(name[i])>tolower(param.name[i])) return false;
		++i;
	}
	if (name.length()<param.name.length()) return true;
	return false;
}
#pragma once

#include <iostream>
#include <fstream>
#include <string>
#include <list>

#include "WebSite.h"

void DoSomething(System::Windows::Forms::RichTextBox^  richTextBox);

namespace HowToInit {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::ComboBox^  comboBox1;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::RichTextBox^  richTextBox1;
//	private: std::list<WebSite> websitesX;
	protected: 

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->comboBox1 = (gcnew System::Windows::Forms::ComboBox());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());
			this->SuspendLayout();
			// 
			// comboBox1
			// 
			this->comboBox1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->comboBox1->FormattingEnabled = true;
			this->comboBox1->Location = System::Drawing::Point(25, 25);
			this->comboBox1->Name = L"comboBox1";
			this->comboBox1->Size = System::Drawing::Size(121, 28);
			this->comboBox1->TabIndex = 0;
			this->comboBox1->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::comboBox1_SelectedIndexChanged);
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->label1->Location = System::Drawing::Point(170, 28);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(51, 20);
			this->label1->TabIndex = 1;
			this->label1->Text = L"label1";
			// 
			// richTextBox1
			// 
			this->richTextBox1->Font = (gcnew System::Drawing::Font(L"Courier New", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->richTextBox1->Location = System::Drawing::Point(25, 90);
			this->richTextBox1->Name = L"richTextBox1";
			this->richTextBox1->Size = System::Drawing::Size(529, 56);
			this->richTextBox1->TabIndex = 2;
			this->richTextBox1->Text = L"";
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(588, 270);
			this->Controls->Add(this->richTextBox1);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->comboBox1);
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion



	private: System::Void comboBox1_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
				 int idx = this->comboBox1->SelectedIndex;
				 System::String ^ txt = this->comboBox1->SelectedItem->ToString();
			 }
	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
				using namespace std;
				std::list<WebSite> websites;
				WebSite * newsite;
				string line;
				std::string lastSite;
				lastSite = "";
				ifstream myfile ("HowToInit.ini");
				if (myfile.is_open())
				{
					while ( myfile.good() )
					{
						getline (myfile,line);
						if (line.size() < 2)
							continue;
						size_t tabpoint = line.find_first_of('\t');
						std::string name = line.substr(0,tabpoint).c_str();
						std::string path = line.substr(tabpoint+1).c_str();
						
						if (name.compare("Last:"))
						{
							newsite = new WebSite(name,path);
							websites.push_back(*newsite);
						}
						else
							lastSite = path;
						
					}
					myfile.close();
				}
				else 
				{
					// create dummy file here
				}
				websites.sort();

				std::list <WebSite>::iterator webSiteIter;

				int i = 0;
				int SelectedIndex = 0;

				for ( webSiteIter = websites.begin(); webSiteIter != websites.end();webSiteIter++)
				{
					this->comboBox1->Items->Add(gcnew String(webSiteIter->name.c_str()));
					std::string showName;
					showName = webSiteIter->name;
					if ( showName.compare(lastSite) == 0 )
					{
						SelectedIndex = i;
						this->label1->Text = gcnew String(webSiteIter->path.c_str() );
					}
					i++;
				}
				this->comboBox1->Items->Add("New Website");
				this->comboBox1->SelectedIndex = SelectedIndex;
			}
	};
}

Start with something like this:

// DW_396087.h

#pragma once

using namespace System;

namespace DW_396087 {

	public ref class WebSite
	{
	public:
		WebSite(void);
		WebSite(String^ domainName, String^ localPath);
		bool operator < (WebSite);

		String^ name;
		String^ path;
	};
}

//...and the body...

#include "stdafx.h"
#include "DW_396087.h"
using namespace System;
namespace DW_396087{

WebSite::WebSite(void)
{
}

WebSite::WebSite(String^ domainName, String^ localPath)
{
	name = domainName;
	path = localPath;	
}

bool WebSite::operator <(WebSite param)
{
	unsigned int i=0;

	if (name->Length < param.name->Length)
		return true;

	String^ strNameCopy = name->ToLower();
	String^ strParamNameCopy = param.name->ToLower();

	while ( ( i < name->Length ) && ( i < param.name->Length ) )
	{
		if (strNameCopy[i] < strParamNameCopy[i])
			return true;
		else if (strNameCopy[i]>strParamNameCopy[i])
			return false;
		++i;	
	}

	return false;
}
}

That less-than operator is kinda weird. What is it actually supposed to determine?

Thanks for the coding help thines, now for your question.

"That less-than operator is kinda weird. What is it actually supposed to determine?"

It is the code that makes line 165 work!

websites.sort();

.sort() uses the < operator to determine if one class preceeds another in the sorted list The original code does a case insensative sort on the domain names of each website.

Try this instead and let me know how it works out when you sort or anything else:

// DW_396087.h

#pragma once

using namespace System;

namespace DW_396087 {

   public ref class WebSite
   {
   public:
      WebSite(void);
      WebSite(String^ domainName, String^ localPath);
      //bool operator < (WebSite);

      String^ name;
      String^ path;

      virtual String^ ToString() override
      {
         return name+'-'+path;
      }

      virtual bool Equals(Object^ obj) override
      {
         return this->ToString()->Equals(((WebSite^)obj)->ToString());
      }

      virtual int GetHashCode() override
      {
         return this->ToString()->GetHashCode();
      }
   };
}

The new code creates a HUGE number of errors when using the old style std::list. I recreated the project and stripped out just about everything to find the correct syntax. It is now just a blank form with an almost empty website class. Creating the list of website classes still causes some errors. What is the proper syntax to create a list of “ref classes”?

I found a far more complex way of doing the list on line, but it still gave me some errors:

private:	System::Collections::Generic::List<website>^ domains =
		gcnew System::Collections::Generic::List<website>;

Output:

1>------ Build started: Project: Sites, Configuration: Debug Win32 ------
1>Compiling...
1>Sites.cpp
1>Form1.h(26) : error C3225: generic type argument for 'T' cannot be 'website', it must be a value type or a handle to a reference type
1>Form1.h(27) : error C3225: generic type argument for 'T' cannot be 'website', it must be a value type or a handle to a reference type
1>Form1.h(26) : error C3845: 'Sites::Form1::domains': only static data members can be initialized inside a ref class or value type
1>Build log was saved at "file://...\Debug\BuildLog.htm"
1>Sites - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Error List:

Error 1 error C3225: generic type argument for 'T' cannot be 'website', it must be a value type or a handle to a reference type	Form1.h	26 Sites
Error 2 error C3225: generic type argument for 'T' cannot be 'website', it must be a value type or a handle to a reference type	Form1.h	27 Sites
Error 3 error C3845: 'Sites::Form1::domains': only static data members can be initialized inside a ref class or value type	Form1.h	26 Sites
#pragma once
using namespace System;

ref class website
{
public:
	website(void);
	String^ domain;
};
#include "StdAfx.h"
#include "website.h"

website::website(void)
{
}
#pragma once

using namespace System;
#include "website.h"

namespace Sites {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	private:	System::Collections::Generic::List<website>^ domains =
					gcnew System::Collections::Generic::List<website>;
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}

	protected: 


	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->SuspendLayout();
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(604, 270);
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->ResumeLayout(false);

		}
#pragma endregion
	private: System::Void comboBox1_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
			 }
	};
}

Are you still using the unmanaged website class?
I noticed it is lower case and has no handle marker^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.