473,573 Members | 2,806 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 multidimensiona l array must have bounds for
all dimensions except the first"

Can someone explain why??

Thanks;
Mehmet Canayaz

Jul 23 '05 #1
6 6568
me*****@gmail.c om 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 multidimensiona l 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 multidimentiona l
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 multidimentiona l
arrays. And in general, prefer using 'vector' over your own allocation
of arrays.
me*****@gmail.c om 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.c om 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::ostre am& 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::cou t, "'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::cou t, "'x' with values added");

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

// dump the contents of 'b'
b.dump(std::cou t, "'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::cou t, "'x' after x.clear()");
b.dump(std::cou t, "'b' after x.clear()");

// copy 'b' to 'x' using the '=' operator
x = b;
x.dump(std::cou t, "'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::cou t, "'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::ostre am& 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::cou t, "'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::cou t, "'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::cou t, "'x' with values added");

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

// dump the contents of 'b'
b.dump(std::cou t, "'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::cou t, "'x' after x.clear()");
b.dump(std::cou t, "'b' after x.clear()");

// copy 'b' to 'x' using the '=' operator
x = b;
x.dump(std::cou t, "'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::cou t, "'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
2054
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 the constant variable and if I changed the value through the pointer, the constant variable should have been modified. Well, that's what I thought...
1
1459
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 private section of another class, and I'd like to instantiate in the class's constructor, with the option of having a size determined at run-time.
35
721
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) I want to dynamically create a multidimensional array and write code to manipulate that. Essentially I have the following setup:
24
2516
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 that I am using them appropriate in class in question. One typical example: This class initiates TCP session, keeps sending commands to the server...
3
1766
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 dim> class Matrix { protected :
9
3007
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
29323
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 called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this...
3
2750
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 matrix into my array of lists then use ./matrix find 1 3 to do something to the matrix I have just made (which is an array of lists) i was going...
112
5386
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 that may print some messages. foo(...) { if (!silent)
0
7679
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8191
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7760
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8049
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5573
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3724
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3723
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2185
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1029
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.