vector problem

S

Simon Cooke

struct Vertex
{
int x,y,z;
};


vector<Vertex> Vertices(3);

Works fine... compiles with no errors:

#include <vector>

using namespace std;

struct Vertex
{
int x,y,z;
};

vector<Vertex> Vertices(3);


int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
 
J

John Carson

Dave said:
Hello again

simple problem. Heres my code:

struct Vertex
{
int x,y,z;
};


vector<Vertex> Vertices(3);

And the error:

error C2926: 'struct main::Vertex' : types with no linkage cannot be
used as template arguments

Comeau online and VC++ .Net 2002 both compile it without a problem.
And yes, when i have some money to spare, i WILL get a decent C++
reference book, just don't have spare $100 at the moment ;)

Free download:

http://mindview.net/Books/TICPP/ThinkingInCPP2e.html
 
W

wogston

struct Vertex
{
int x,y,z;
};


vector<Vertex> Vertices(3);

And the error:

error C2926: 'struct main::Vertex' : types with no linkage cannot be used as
template arguments

Put the Vertex declaration outside of main function, then try again.
 
D

David Fisher

struct Vertex
used

Put the Vertex declaration outside of main function, then try again.

Yep ... "external linkage" is referring to the fact that Vertex is a locally
defined type.

If you get an error message you don't understand, try searching for it on
http://www.google.com
to see if someone else has had the same problem - if you type in this one,
you get a page saying:

"If you are using Visual C++, you will get the following error message:
'struct main::student_record' : types with no linkage cannot be used as
template arguments.
The essential difficulty here is that the class student_record is
defined locally in main()
and the vector class can't see it."

David F
 
J

John Carson

Dave said:
ok i put


outside the main function, after the includes, and it worked. can
someone explain why? why, it seems, it doesn't accept local types in
the vector declaration?

cheers
dave

Because the (1998) standard says so. Section 14.3.1 p2:

"A local type, a type with no linkage, an unnamed type or a type compounded
from any of these types shall not be used as a template-argument for a
template type-parameter. [Example:
template <class T> class X { /* ... */ };
void f()
{
struct S { /* ... */ };
X<S> x3; // error: local type used as template-argument
X<S*> x4; // error: pointer to local type used as template-argument
}
-end example]"
 
A

Andre Kostur

struct Vertex
{
int x,y,z;
};


vector<Vertex> Vertices(3);

Works fine... compiles with no errors:

#include <vector>

using namespace std;

struct Vertex
{
int x,y,z;
};

vector<Vertex> Vertices(3);


int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}

_tmain ?!?! Where's that in the standard? :)
 
L

lilburne

Dave said:
that seems a bit annoying and pointless. is there any particular reason for
that rule?

Requiring compilers to support them in templates was
probably considered too much of an overhead for something
that has always seemed to be a bit of a curiosity. Though no
doubt someone has an application that would be impossible to
code without them.
 
J

John Carson

Dave said:
that seems a bit annoying and pointless. is there any particular
reason for that rule?

:)
dave


I'm sure there is but it would take an expert on C++ compilers to give you
the details. The basic point is that defining a language involves trade-offs
between features and speed. Providing support for every language feature
that some user thinks ought to be there makes for a much more complicated
compiler and slower running applications.
 
D

Dave

Hello again

simple problem. Heres my code:

struct Vertex
{
int x,y,z;
};


vector<Vertex> Vertices(3);

And the error:

error C2926: 'struct main::Vertex' : types with no linkage cannot be used as
template arguments

And yes, when i have some money to spare, i WILL get a decent C++ reference
book, just don't have spare $100 at the moment ;)

cheers
dave
 
D

Dave

ok i put

outside the main function, after the includes, and it worked. can someone
explain why? why, it seems, it doesn't accept local types in the vector
declaration?

cheers
dave
 
D

Dave

that seems a bit annoying and pointless. is there any particular reason for
that rule?

:)
dave
 

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
474,147
Messages
2,570,835
Members
47,382
Latest member
MichaleStr

Latest Threads

Top