Jump to content
  • Advertisement
Sign in to follow this  
Thursgun

[c++] a problem with object

This topic is 3650 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

========================= ICPC.cpp =============================================
#include <iostream> 
#include <fstream> 
#include <cstring>
#include <string>

using namespace std;

typedef unsigned int uint;

#include "Datos.cpp"

void leer() 
{

	string linea;
  	char *palabra, *aux;
  	uint number, flag = 0;

  	ifstream archivo ("cows.in");
  	if (archivo.is_open()) 
  	{
    		while (!archivo.eof()) 
    		{
      			getline(archivo,linea);
      			aux = new char [linea.size()+1];
      			strcpy (aux, linea.c_str());
      			palabra = strtok(aux," ");

      			while (palabra) 
      			{
        			number = atoi(palabra);
				if (flag == 0) 
				{
					Datos pruebas = new Datos(number); 
					flag = 1;
				}
				else
					
				cout << number << endl;
	
				palabra = strtok(NULL," ");
      			}

    		}
     	archivo.close();
  	}
  	else cout << "error"; 
}

int main() 
{
	leer();
  	return 0;
}
============================= Datos.cpp ================================================
//typedef unsigned int uint; 
 
class Datos {

	//atributos
	private:
	uint lineas; 
	uint i;      
	public:
	uint *PSP; //Puzzle Solving Power

	//constructor
	Datos(uint T);

	//destructor
	~Datos();

	//metodos
	void inserta(uint dato); 
	uint obtener(uint i);    
};

Datos::Datos(uint T) {
	lineas = T*36;
	i = 0;
	PSP = new uint [lineas];
}

Datos::~Datos() {
	delete[] PSP;
}

void Datos::inserta(uint dato) { 
	PSP = dato;
	i++;
}

uint Datos::obtener(uint i) { 
	uint dato;	
	dato = PSP;
	return dato;
}
========================================================================================== I'm trying to read an input file called "cows.in" and save every number in the PSP array using the class "Datos.cpp". This is the error I get at compile time: ICPC.cpp: In function ‘void leer()’: ICPC.cpp:34: error: invalid conversion from ‘Datos*’ to ‘uint’ ICPC.cpp:34: error: initializing argument 1 of ‘Datos::Datos(uint)’ It says that the problem is here: ... if (flag == 0) { Datos pruebas = new Datos(number); //<-ERROR HERE ... My question is: "What's the problem?". Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
pruebas needs to be a pointer type:
Datos* pruebas = new Datos(number);
I think what's happening now is that the compiler is trying to perform an implicit conversion from a Datos* to a uint through the single-argument Datos constructor, hence the somewhat cryptic error.

Ah, C++...

Share this post


Link to post
Share on other sites
#include "Datos.cpp"
Explain this to me.

while (!archivo.eof())
Explain what you think eof does. Remember that archivo is a stream.

Datos pruebas = new Datos(number);
Datos *pruebas. Are you familiar with pointers, new, and delete?

Also, consider using C++ strings. Why use part of C++, and not strings?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!