double array code fails

C

CodeGrommet

Hi all. The following code gives the error below. I'm coding against
minGW. I would appreciate it if someone could tell me where I have
gone wrong.

/*********************************** code
*******************************************/
#include <iostream>
using namespace std;

int main(){
string dogs[][] = { {"terry", "brown"} ,
{"Kristin", "white"} ,
{"toby", "gray"},
{"fido", "black"}
};
cout<<dogs[0][0]<<endl;
}

/******************************** error message
**********************************/

C:\unisa\MyFiles\Test_bin\simple_double_array_output.cpp
[Warning] In function `int main()':
error C:\unisa\MyFiles\Test_bin\simple_double_array_output.cpp:5
declaration of `dogs' as multidimensional array must have bounds for
all dimensions except the first
error C:\unisa\MyFiles\Test_bin\simple_double_array_output.cpp:10
`dogs' undeclared (first use this function)
[Build Error] (Each undeclared identifier is reported only once for
each function it appears in.)

/
************************************************************************************/
 
R

Ron AF Greve

Hi,


Just doing what the error message told you to do (and adding string as
header):


#include <iostream>
#include <string>
using namespace std;

int main(){
string dogs[][2] = { {"terry", "brown"} ,
{"Kristin", "white"} ,
{"toby", "gray"},
{"fido", "black"}
};
cout<<dogs[0][0]<<endl;
cout<<dogs[0][1]<<endl;
}



Regards, Ron AF Greve

http://www.InformationSuperHighway.eu
 
R

Ron AF Greve

Hm,

and it would be a good habbit to add

return 0;

At the end (indicating no error as return value)


Regards, Ron AF Greve

http://www.InformationSuperHighway.eu

Ron AF Greve said:
Hi,


Just doing what the error message told you to do (and adding string as
header):


#include <iostream>
#include <string>
using namespace std;

int main(){
string dogs[][2] = { {"terry", "brown"} ,
{"Kristin", "white"} ,
{"toby", "gray"},
{"fido", "black"}
};
cout<<dogs[0][0]<<endl;
cout<<dogs[0][1]<<endl;
}



Regards, Ron AF Greve

http://www.InformationSuperHighway.eu

CodeGrommet said:
Hi all. The following code gives the error below. I'm coding against
minGW. I would appreciate it if someone could tell me where I have
gone wrong.

/*********************************** code
*******************************************/
#include <iostream>
using namespace std;

int main(){
string dogs[][] = { {"terry", "brown"} ,
{"Kristin", "white"} ,
{"toby", "gray"},
{"fido", "black"}
};
cout<<dogs[0][0]<<endl;
}

/******************************** error message
**********************************/

C:\unisa\MyFiles\Test_bin\simple_double_array_output.cpp
[Warning] In function `int main()':
error C:\unisa\MyFiles\Test_bin\simple_double_array_output.cpp:5
declaration of `dogs' as multidimensional array must have bounds for
all dimensions except the first
error C:\unisa\MyFiles\Test_bin\simple_double_array_output.cpp:10
`dogs' undeclared (first use this function)
[Build Error] (Each undeclared identifier is reported only once for
each function it appears in.)

/
************************************************************************************/
 
G

Guest

Hi all. The following code gives the error below. I'm coding against
minGW. I would appreciate it if someone could tell me where I have
gone wrong.

What part of
declaration of `dogs' as multidimensional array must have bounds for
all dimensions except the first

is it that you do not understand?

BTW: you need to include <string> to get the printing to work.
 
R

robin

Hi all. The following code gives the error below. I'm coding against
minGW. I would appreciate it if someone could tell me where I have
gone wrong.

/*********************************** code
*******************************************/
#include <iostream>
using namespace std;

int main(){
string dogs[][] = { {"terry", "brown"} ,
{"Kristin", "white"} ,
{"toby", "gray"},
{"fido", "black"}
};
cout<<dogs[0][0]<<endl;

}

/******************************** error message
**********************************/

C:\unisa\MyFiles\Test_bin\simple_double_array_output.cpp
[Warning] In function `int main()':
error C:\unisa\MyFiles\Test_bin\simple_double_array_output.cpp:5
declaration of `dogs' as multidimensional array must have bounds for
all dimensions except the first
error C:\unisa\MyFiles\Test_bin\simple_double_array_output.cpp:10
`dogs' undeclared (first use this function)
[Build Error] (Each undeclared identifier is reported only once for
each function it appears in.)

/
***************************************************************************­*********/

Yeah, in your code the second dimension must be specified. In C++ the
"multidimensional arrays" are actually "arrays of arrays", so your
code is intended to create an array(dimension 1) of 4 elements where
each element contains an array(dimension 2) of 2 elements that are
std::string's. Compiler needs to know how long each dimension-2 array
is in order to determine the size of each dimension-1 array element.
 
J

James Kanze

btw, the code runs without the string include.

But it's undefined behavior. In order to use std::string (in
any way---not just for output), you must include <string>, or
you have undefined behavior.

In practice, you don't have to worry about most of the problems
of undefined behavior; in practice, either your program will
compile and work as expected, or it will fail to compile.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top