473,414 Members | 1,980 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

global variable matrix

#include <iostream>
using namespace std;

int x[][]; //global variable matrix

int main()
{
x= new float [1][2]; //initialize the size now

}

I am getting the error
" declaration of `x' as multidimensional array must have bounds for
all dimensions except the first"

Can someone explain why??

Thanks;
Mehmet Canayaz

Jul 23 '05 #1
6 6553
me*****@gmail.com wrote:
#include <iostream>
using namespace std;

int x[][]; //global variable matrix

int main()
{
x= new float [1][2]; //initialize the size now

}

I am getting the error
" declaration of `x' as multidimensional array must have bounds for
all dimensions except the first"

Can someone explain why??


You declared 'x' to be a multi- (two-) dimensional array. The language
rules require such a declaration to contain the sizes of all dimensions
except the first (the closest to the variable). You didn't provide
any sizes. The compiler cannot accept that, and therefore complains.

Please read the FAQ about dynamic memory management and multidimentional
arrays. And in general, prefer using 'vector' over your own allocation
of arrays.

V
Jul 23 '05 #2
Ok. That is what the error message was telling me and I just wanted to
make sure. Now, I have another question:

let's say that I have an implementation as follows

#include <iostream>
using namespace std;

int x [ ][ ] ; // global variable matrix
int len_x; // will be the length of the array in x direction

// modify_ar modifies the array
void modify_ar (int a, int b, ar[][]){
ar[a][b] = 5;
}

int main()
{

scanf("%d", len_x); //read the length now
x= new float [len_x][2]; //initialize the matrix now

}
So as I tried to show above, I need to read an integer from a file and
create a matrix using that integer as a length. In addition, there is
this function modify_ar I will use to modify the array I just created.

Thanks ,
Mehmet Canayaz

Jul 23 '05 #3
Victor Bazarov wrote:
Please read the FAQ about dynamic memory management and multidimentional
arrays. And in general, prefer using 'vector' over your own allocation
of arrays.
me*****@gmail.com wrote:
Ok. That is what the error message was telling me and I just wanted to
make sure. Now, I have another question:

It might be helpful to follow Victor's advice first.

--
CrayzeeWulf
Jul 23 '05 #4
Ok. Thanks :)

Jul 23 '05 #5
me*****@gmail.com wrote:
Ok. That is what the error message was telling me and I just wanted to
make sure. Now, I have another question:

let's say that I have an implementation as follows

#include <iostream>
using namespace std;

int x [ ][ ] ; // global variable matrix
You must specify all but the first dim of 'x', e.g.
int x[][2];
int len_x; // will be the length of the array in x direction

// modify_ar modifies the array
void modify_ar (int a, int b, ar[][]){
You must specify all but the first dim of 'ar', e.g.
void modify_ar(int a, int b, ar[][2]) {
ar[a][b] = 5;
}

int main()
{

scanf("%d", len_x); //read the length now
x= new float [len_x][2]; //initialize the matrix now
Hmm, you declare 'x[][]' as 'int', now you want it to
be 'float' - it can be one or the other, but not both.

}
So as I tried to show above, I need to read an integer from a file and
create a matrix using that integer as a length. In addition, there is
this function modify_ar I will use to modify the array I just created.

Thanks ,
Mehmet Canayaz


Use 'vector' instead:

//-------------------------

#include <vector>
#include <iostream>
// class Matrix derived from a vector of vectors of T.
// all of the features of 'vector' are available to Matrix.
template< class T >
class Matrix : public std::vector< std::vector< T > >
{
public:
Matrix() : std::vector< std::vector< T > >()
{
}

Matrix(int row, int col) :
std::vector< std::vector< T > >(row, std::vector< T >(col))
{
}

Matrix(const Matrix& m) : std::vector< std::vector< T > >(m)
{
}

// dump my contents to 'os' for debugging
std::ostream& dump(std::ostream& os, const char * title = NULL)
{
if (title)
os << title << std::endl;

for (int row = 0; row < size(); row++)
{
os << "row: " << row << std::endl;
for (int col = 0; col < this->operator[](row).size(); col++)
os << " " << this->operator[](row)[col];
os << std::endl;
}

os << std::endl;

return os;
}
};

int main()
{
int rows;

// read 'rows' from stdin
std::cerr << "Enter number of matrix rows: ";
std::cin >> rows;

// we could also read the number of columns
// from stdin, but in this example we've
// hard-coded the column counts.

// make 'x' as a 2 dim matrix of ints with
// 'rows' rows and 2 cols
Matrix<int> x(rows, 2);

// dump the initial contents of 'x' - should be
// all zeroes
x.dump(std::cout, "'x' initial contents");

int v = 0;

// put some values into 'x'
for (int row = 0; row < x.size(); row++)
for (int col = 0; col < x[row].size(); col++)
x[row][col] = v++;

// dump the new contents of 'x'
x.dump(std::cout, "'x' with values added");

// make 'b' as a copy of 'x'
Matrix<int> b(x);

// dump the contents of 'b'
b.dump(std::cout, "'b' as a copy of 'x'");

// erase 'x'
x.clear();

// demonstrate that the copy-constructor
// actually copied the data from 'x' to 'b'
x.dump(std::cout, "'x' after x.clear()");
b.dump(std::cout, "'b' after x.clear()");

// copy 'b' to 'x' using the '=' operator
x = b;
x.dump(std::cout, "'x' after 'x = b'");

// make 'c' as a 2 dim matrix of floats with
// 'rows' rows and 4 cols
Matrix<double> c(rows, 4);
c.dump(std::cout, "'c' of doubles");

return 0;
}

Regards,
Larry

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #6
Embedded code additions show how to make a global
matrix of ints that is resized at runtime.

Larry I Smith wrote:
[snip]

Use 'vector' instead:

//-------------------------

#include <vector>
#include <iostream>
// class Matrix derived from a vector of vectors of T.
// all of the features of 'vector' are available to Matrix.
template< class T >
class Matrix : public std::vector< std::vector< T > >
{
public:
Matrix() : std::vector< std::vector< T > >()
{
}

Matrix(int row, int col) :
std::vector< std::vector< T > >(row, std::vector< T >(col))
{
}

Matrix(const Matrix& m) : std::vector< std::vector< T > >(m)
{
}

// dump my contents to 'os' for debugging
std::ostream& dump(std::ostream& os, const char * title = NULL)
{
if (title)
os << title << std::endl;

for (int row = 0; row < size(); row++)
{
os << "row: " << row << std::endl;
for (int col = 0; col < this->operator[](row).size(); col++)
os << " " << this->operator[](row)[col];
os << std::endl;
}

os << std::endl;

return os;
}
};

// 'a' is a 2 dim matrix of ints with zero rows and cols
Matrix<int> a;
int main()
{
int rows;

// read 'rows' from stdin
std::cerr << "Enter number of matrix rows: ";
std::cin >> rows;

// read 'cols' from stdin
std::cerr << "Enter number of matrix columns: ";
std::cin >> cols;

// resize the global 'a' to have 'rows' rows and
// 'cols' columns
a.resize(rows, std::vector<int>(cols));
a.dump(std::cout, "'a' after resize()");
// we could also read the number of columns
// from stdin, but in this example we've
// hard-coded the column counts.

// make 'x' as a 2 dim matrix of ints with
// 'rows' rows and 2 cols
Matrix<int> x(rows, 2);

// dump the initial contents of 'x' - should be
// all zeroes
x.dump(std::cout, "'x' initial contents");

int v = 0;

// put some values into 'x'
for (int row = 0; row < x.size(); row++)
for (int col = 0; col < x[row].size(); col++)
x[row][col] = v++;

// dump the new contents of 'x'
x.dump(std::cout, "'x' with values added");

// make 'b' as a copy of 'x'
Matrix<int> b(x);

// dump the contents of 'b'
b.dump(std::cout, "'b' as a copy of 'x'");

// erase 'x'
x.clear();

// demonstrate that the copy-constructor
// actually copied the data from 'x' to 'b'
x.dump(std::cout, "'x' after x.clear()");
b.dump(std::cout, "'b' after x.clear()");

// copy 'b' to 'x' using the '=' operator
x = b;
x.dump(std::cout, "'x' after 'x = b'");

// make 'c' as a 2 dim matrix of doubles with
// 'rows' rows and 4 cols
Matrix<double> c(rows, 4);
c.dump(std::cout, "'c' of doubles");

return 0;
}

Regards,
Larry

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: CoolPint | last post by:
While I was reading about const_cast, I got curious and wanted to know if I could modify a constant variable through a pointer which has been "const_cast"ed. Since the pointer would be pointing to...
1
by: aurgathor | last post by:
Howdy, I got the templatized class Matrix working in the way it's written in the faq , and it works fine as: Matrix<sqr_T> display(80,25); However, I'd like to have this variable in the...
35
by: whisper | last post by:
My question is whether it is better to use a global variable to hold a dynamically malloced multidim array or pass around pointers to it. The details are below (forgive the long winded explanation)...
24
by: LP | last post by:
After a code review one coworker insisted that global are very dangerous. He didn't really give any solid reasons other than, "performance penalties", "hard to maintain", and "dangerous". I think...
3
by: martin.druon | last post by:
Hi, I created a template class to represent hypermatrix. I would like to add methods where the number of parameters are checked during the compilation time. For example : template <size_t...
9
by: Ed Jensen | last post by:
I'm having a vexing problem with global variables in Python. Please consider the following Python code: #! /usr/bin/env python def tiny(): bar = for tmp in foo: bar.append(tmp) foo = bar
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
3
by: dn6326 | last post by:
hi, essentially, my program needs to store an array of lists so that in main() it can have 2 run modes from the command line. i.e. i can run the program with ./matrix load filename to load a...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.