cannot appear in a constant-expression (list)

Hi.
Why do I get the error
'rec' cannot appear in a constant-expression
?

I have the following definitions:
1
2
3
  ...  string rec[6];
    list<rec> musicList;
...
list (I'm assuming it's a std::list) expects a typename for its (first) template parameter. You're giving it an array of strings.
Yes, an std::list.
What should I do?
It depends on what you want to do, which you haven't specified. I'm going to assume you want to make a list of strings, that would be list<string> musicList; as string names the type of object to make a list of.
I am going to make a list of 6 string( an array of 6 strings.I can make an array of lists of strings, but that is not what I am up to
You can try using std::array:

1
2
std::array<std::string, 6> rec;
std::list< std::array<std::string, 6> > musicList;


This requires C++11 libraries, so you might need to enable them.
What's wrong with:
1
2
3
4
5
struct A {
   int array[2];
} ;

std::list <A> alist;


? I want something C++ 98
Nothing is wrong with that. But, there is something wrong with trying to use a variable instead of a type as the template type.

Though how did you go from an array of 6 strings to an array of 2 ints :P

Also, if that is something you want then do it we can't stop you.

You don't stop me, the compiler does.

Here is What I did:
1
2
3
4
5
6
7
8
    string rec[3];
    
    rec[0] = "ali";
    rec[1] = "hasan";
    rec[2] = "hossein";

std::list <string[3]> alist;
alist.push_back(rec);

But the last line errs.....................
You don't want a list of an array of 3 strings, you want a list or strings, right? So that gives you:
1
2
3
4
std::list<std::string> alist;
alist.push_back("ali");
alist.push_back("hasan");
alist.push_back("hossein");

kbw (7000)
You don't want a list of an array of 3 strings, you want a list or strings, right?

No.I want a list of arrays. I mean each line of list should have 3 parts
Last edited on
A list of arrays of strings?
1
2
3
4
5
6
7
8
9
10
#include <string>
#include <list>

// either
#include <array>
std::list<std::array<std::string, 3>> alist; // list of arrays of 3 strings

// or (C++98)
#include <vector>
std::list<std::vector<std::string>> alist; // list of dynamic arrays of strings 

EDIT: sorry, missed your earlier post about C++11. But, you should consider using it - it has been around for 3 years now, most compilers should support it.
Last edited on
If you want something C++98, then the struct approach you had earlier will work if you use strings instead of ints.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct A
{  string rec[3];
};
A                   a;
std::list<A>  alist;  

a.rec[0] = "ali";
a.rec[1] = "hasan";
a.rec[2] = "hossein";
alist.push_back (a); 
std::list<A>::iterator iter = alist.begin();
std::cout << iter->rec[0] << std::endl;
std::cout << iter->rec[1] << std::endl;
std::cout << iter->rec[2] << std::endl;




Last edited on
AbstractionAnon (2459)
If you want something C++98, then the struct approach you had earlier will work if you use strings instead of ints.

1
2
3
4
5
6
7
8
9
10
11
12
13
14



struct A
{ string rec[3];
};
A a;
std::list<A> alist;

a.rec[0] = "ali";
a.rec[1] = "hasan";
a.rec[2] = "hossein";
alist.push_back (a);
std::list<A>::iterator iter = alist.begin();
std::cout << iter->rec[0] << std::endl;
std::cout << iter->rec[1] << std::endl;
std::cout << iter->rec[2] << std::endl;



I copied your code and pasted it into my main() function.This is the error I get:
21 G:\Test_suite\HW2_Hooshdaran.cpp `main()::A' uses local type `main()::A'
I got no such error.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string> 
#include <list> 
#include <iostream>
using namespace std; 

struct A
{ string rec[3];
};

int main ()
{	A a;
	std::list<A> alist; 

	a.rec[0] = "ali";
	a.rec[1] = "hasan";
	a.rec[2] = "hossein";
	alist.push_back (a); 
	std::list<A>::iterator iter = alist.begin();
	std::cout << iter->rec[0] << std::endl;
	std::cout << iter->rec[1] << std::endl;
	std::cout << iter->rec[2] << std::endl;
}
ali
hasan
hossein
Press any key to continue . . .
Thanks.I also get no error now that I moved the definition of A outside of main() scope.
It should make no difference if A is defined inside or outside of the main scope. Just as long as it's defined before the use of it at line 11.
If it is defined inside of a function is it scoped to that function as the "error" or "warning" you got. Meaning you can't use it outside of that function.
Topic archived. No new replies allowed.