4 Replies - 8131 Views - Last Post: 27 March 2010 - 08:42 PM

#1 dranfu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 24-November 08

Type Casting Error Using Array List - Visual C++

Posted 26 March 2010 - 09:21 PM

Hi,

I'm just learning C++ on my own. I'm an MIS student, and I've done some VB.net programming (way easy) and some Java, too. However, C++ is not being taught in the program I'm in, so I am trying to learn it on my own. It's a much more complicated language. The problem I am having is with this code:

using namespace System;
using namespace System::IO;
using namespace System::Collections;
using namespace std;

int main () {
	
	//create the input, output, and string objects
	ifstream readFile ("BigList.txt");
	ofstream writeFile("NewList.txt");
	string aLine;
	ArrayList textArray = gcnew ArrayList();

	if (readFile.is_open()) {

		while (! readFile.eof())
			{
				getline (readFile, aLine);
				textArray.Add(aLine);

			}// end while

		} //end if
	else 
		{
			...some other code
		}

	

	} //end main





When compiled, I receive this error: stdafx.cpp(30) :
error C2664: 'System::Collections::ArrayList::Add' : cannot convert parameter 1 from 'std::string' to 'System::Object ^'
1>        No user-defined-conversion operator available, or
1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called 


I'm simply trying to read some lines from a text file and add them one by one into an array list. I am doing this in Visual C++, btw. This was very easy in Visual Basic.net, but it is not as easy in C++.

As far as I can tell, the problem is that the array list expects an object, not a string type, and so won't allow me to add it to the array list. But I've tried about 5 different methods to cast the string being added to the array list as an object, and they all fail. I'm sure I am missing something; maybe a lot of somethings, but I was hoping someone could point me in the right direction.

Thank u.

Is This A Good Question/Topic? 0
  • +

Replies To: Type Casting Error Using Array List - Visual C++

#2 eker676   User is offline

  • Software Engineer
  • member icon

Reputation: 379
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Type Casting Error Using Array List - Visual C++

Posted 26 March 2010 - 09:33 PM

If you are learning C++ then you need to start with standard C++ not Microsoft's Managed C++.

Onto your problem, try adding a line like this:
textArray.Add(S(aLine));

Was This Post Helpful? 0
  • +
  • -

#3 dranfu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 24-November 08

Re: Type Casting Error Using Array List - Visual C++

Posted 26 March 2010 - 10:50 PM

View Posteker676, on 26 March 2010 - 08:33 PM, said:

If you are learning C++ then you need to start with standard C++ not Microsoft's Managed C++.

Onto your problem, try adding a line like this:
textArray.Add(S(aLine));


Hi eker676,

Thank u for your reply. I tried that before, and the error I received was this:

 
1>.\stdafx.cpp(30) : error C3861: 'S': identifier not found



Not sure if there is something that needs to be declared first, but it's not recognizing that S as any kind of identifier. Also, I did look into some ways to do this with just standard C++, but couldn't find anything (not saying it doesn't exist) that is similar to an arrayList in standard C++. The list I want to import can change in size, in terms of the number of characters per line, as well as the number of lines total in the file. Therefore, I don't think I can use an array, as I have to declare the arrays size beforehand.
Was This Post Helpful? 0
  • +
  • -

#4 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Type Casting Error Using Array List - Visual C++

Posted 27 March 2010 - 12:43 AM

Here's one way to do what you were trying to do: :)

// arraylist.cpp : main project file.

#include "stdafx.h"

using namespace System;
using namespace System::IO;
using namespace System::Collections;

int main(array<System::String ^> ^args)
{
	////create the input, output, and string objects
	//ifstream readFile ("ReadMe.txt");
	////ofstream writeFile("NewList.txt");
	//string aLine;
	//ArrayList textArray = gcnew ArrayList();

	//if (readFile.is_open()) {
	//	while (getline (readFile, aLine))	{
	//		textArray.Add(aLine);
	//} //end if
	//else 
	//{
	//	; //...some other code
	//}

	ArrayList ^ textArray = gcnew ArrayList();

	try
	{
		// Create an instance of StreamReader to read from a file.
		StreamReader^ sr = gcnew StreamReader( "ReadMe.txt" );
		try
		{
			String^ line;

			// Read and display lines from the file until the end of 
			// the file is reached.
			while ( line = sr->ReadLine() )
			{
				textArray->Add(line);
			}
		}
		finally
		{
			if ( sr )
				delete (IDisposable^)sr;
		}
	}
	catch ( Exception^ e ) 
	{
		// Let the user know what went wrong.
		Console::WriteLine( "The file could not be read:" );
		Console::WriteLine( e->Message );
	}



	for (int i = 0; i < textArray->Count; i++)
		Console::WriteLine(textArray[i]);

	Console::WriteLine("Hit ENTER to continue...");
	Console::ReadLine();
	return 0;
}



See ArrayList Class and StreamReader Class


Be sure to click the Language Filter dropdown just above the Class Name and select C++
Was This Post Helpful? 0
  • +
  • -

#5 dranfu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 24-November 08

Re: Type Casting Error Using Array List - Visual C++

Posted 27 March 2010 - 08:42 PM

Hi n8wxs,

Thank u so much. That worked like a charm, once I included the
 #using <mscorlib.dll>
header. Is it necessary to use this ^ identifier every time we declare any array list, or a stream reader object? Is there not a way to do this with standard c++?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1