Advertisement Jump to content
Sign in to follow this  

[c++] a problem with object

This topic is 3862 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 ("");
  	if (archivo.is_open()) 
    		while (!archivo.eof()) 
      			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;
				cout << number << endl;
				palabra = strtok(NULL," ");

  	else cout << "error"; 

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

	uint lineas; 
	uint i;      
	uint *PSP; //Puzzle Solving Power

	Datos(uint T);


	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;

uint Datos::obtener(uint i) { 
	uint dato;	
	dato = PSP;
	return dato;
========================================================================================== I'm trying to read an input file called "" 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
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, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!