Foros del Web » Programación para mayores de 30 ;) » C/C++ »

array problema

Estas en el tema de array problema en el foro de C/C++ en Foros del Web. hola tengo un problema con unarray solo esta recogiendo el primer caracter Código PHP: //Vector temporal #define WIDTH 5 #define HEIGHT 3 char *  tabla  [ HEIGHT ][ WIDTH ]; ...
  #1 (permalink)  
Antiguo 13/10/2009, 06:28
Avatar de jamesjara  
Fecha de Ingreso: mayo-2008
Ubicación: san jose
Mensajes: 449
Antigüedad: 16 años
Puntos: 7
array problema

hola tengo un problema con unarray solo esta recogiendo el primer caracter

Código PHP:
//Vector temporal
#define WIDTH 5
#define HEIGHT 3
chartabla [HEIGHT][WIDTH];
int row,col;
tabla[0][0] = '1';
tabla[0][1] = '2';  
tabla[0][2] = '3';  
tabla[0][3] = '4';  
tabla[0][4] = '5'
tabla[1][0] = '6';
tabla[1][1] = '7';  
tabla[1][2] = '8';  
tabla[1][3] = '9';  
tabla[1][4] = '10'
tabla[2][0] = '11';
tabla[2][1] = '12';  
tabla[2][2] = '13';  
tabla[2][3] = '14';  
tabla[2][4] = '15'


table->setRowCount(HEIGHT);
table->setColumnCount(WIDTH);
for (
row=0;row<HEIGHT;row++)
for (
col=0;col<WIDTH;col++)
{
QString text personas_tabla[row][col];
//solo recoge el primer caracter
table->setItem(row col, new QTableWidgetItem(text));  
 } 
__________________
X7CLOUD El webservice latino!
Compatible
con mysql,sql,sqlitte.
Compatible con extjs , sencha , smargwt, Gwt , Jquery , Charts, Streaming.
  #2 (permalink)  
Antiguo 13/10/2009, 08:53
 
Fecha de Ingreso: mayo-2009
Mensajes: 13
Antigüedad: 15 años
Puntos: 0
Respuesta: array problema

¿Qué compilador usas?

Que es persona_tabla? y table?
  #3 (permalink)  
Antiguo 13/10/2009, 08:58
Avatar de r0d
r0d
 
Fecha de Ingreso: noviembre-2007
Mensajes: 86
Antigüedad: 16 años, 5 meses
Puntos: 3
Respuesta: array problema

Hola. Veo 2 errores:
1. en la linea:
Código:
QString text = personas_tabla[row][col];
la variable personas_tabla no existe. Supongo que es tabla.

2. Si tabla es un array (de 2 dimensiones) de char*, eso no vale:
Código:
tabla[0][0] = '1';
porque tabla[0][0] (0,0 o lo que sea) est un char*, pero '1' est un char.
Tienes que escribir:
Código:
tabla[0][0] = "1";
Porque "1" es un char*.

Pero eso:
Código:
char* tabla [HEIGHT][WIDTH];
es C, no es C++.
En c++, en vez de eso, utilizamos los contenedores de la STL.

Aqui tienes un ejemplo de buena manera de hacer en c++: un objeto Dyn2dMatrix (matriz dinamica de 2 dimensiones)
Código:
#ifndef __H_DYN_2D_MATRIX_H__
#define __H_DYN_2D_MATRIX_H__

#include <vector>
#include <string>
#include <algorithm>

template <typename T>
class Dyn2dMatrix
{
public:
	//! contructor
	Dyn2dMatrix(const size_t nbRow = 0, const size_t nbCol = 0)
		: m_nbRow(nbRow)
		, m_nbCol(nbCol)
		, m_array(nbRow*nbCol)
	{}

	//! copy constructor
	Dyn2dMatrix( const Dyn2dMatrix<T> & matrix )
		: m_nbRow( matrix.m_nbRow )
		, m_nbCol( matrix.m_nbCol )
		, m_array( std::vector<T>( matrix.Size() ) )
	{
		std::copy( matrix.m_array.begin(), matrix.m_array.end(), m_array.begin() );
	}

	//! assignment operator
	void operator = ( const Dyn2dMatrix & matrix )
	{
		m_nbRow = matrix.m_nbRow;
		m_nbCol = matrix.m_nbCol;
		m_array = std::vector<T>( matrix.Size() );
		std::copy( matrix.m_array.begin(), matrix.m_array.end(), m_array.begin() );
	}

	//! resize the matrix and refill all with the given value
	void Resize(const size_t nbRow, const size_t nbCol, const T& value)
	{
		m_nbRow = nbRow;
		m_nbCol = nbCol;
		m_array.resize( m_nbRow * m_nbCol, value );
	}

	//! destructor
	~Dyn2dMatrix()
	{
		m_array.clear();
	}

	//! accessor
	T operator () ( const size_t col, const size_t row ) const
	{
		return m_array[m_nbCol*row+col];
	}

	//! get matrix width ( nb col )
	size_t Width() const { return m_nbCol; }

	//! get matrix height ( nb row )
	size_t Height() const { return m_nbRow; }

	//! total number of elements
	size_t Size() const { return m_array.size(); }

	//! mutator
	T & operator () ( const size_t col, const size_t row )
	{
		return m_array[m_nbCol*row+col];
	}

	//! mutator
	T & operator () ( const size_t pos )
	{
		return m_array[pos];
	}

	//! fill the matrix with the same value
	void Fill( const T & value )
	{
		std::fill( m_array.begin(), m_array.end(), value);
	}

	//! replace element
	void Replace( const T & before, const T & after )
	{
		for ( size_t i = 0; i<m_array.size(); i++ )
			if ( m_array[i] == before )
				m_array[i] = after;
	}

	//! ToString
	std::string ToString() const
	{
		std::string strRet;
		for ( size_t i = 0; i<m_array.size(); i++ )
		{
			strRet += Tools::ToString<T>( m_array[i] );
			if ( ( (i+1) % m_nbCol) == 0 )
				strRet += "\n";
		}
		return strRet;
	}

private:
	//! matrix size
	size_t m_nbRow, m_nbCol;

	//! datas
	std::vector<T> m_array;
};

#endif // __H_DYN_2D_MATRIX_H__
__________________
Alicia: Sólo quiero saber que camino debo tomar.
Gato risón: Pues... depende mucho de donde quieras ir.

Mi página web
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 12:01.