error in my vector program...

  • Thread starter =?ISO-8859-1?Q?Martin_J=F8rgensen?=
  • Start date
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Hi,

I get this using g++:

main.cpp:9: error: new types may not be defined in a return type
main.cpp:9: note: (perhaps a semicolon is missing after the definition
of 'vector')
main.cpp:9: error: two or more data types in declaration of 'set'

I don't really see the problem... Here's the code:

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

struct vector
{
double x, y, z;
}

void set(vector & v, double x1, double y1, double z1)
{
v.x = x1; v.y = y1; v.z = z1;
}

void scale(vector & v, double k)
{
v.x *= k; v.y *=k; v.z *=k;
}

double inner(vector a, vector b)
{
return a.x * b.x + a. y * b.y + a.z * b.z;
}

void print(vector v)
{
cout << "Vector(" << v.x << ", " << v.y << ", " << v.z << ")" << endl;
}

int main()
{
vector v1;
vector v2;
set(v1, 1.1, 2.2, 3.3);
v2=v1;
scale(v2, 2);
print(v1); print(v2);
cout << "Inner product = " << inner(v1, v2);
}
 
O

osmium

:

I get this using g++:

main.cpp:9: error: new types may not be defined in a return type
main.cpp:9: note: (perhaps a semicolon is missing after the definition of
'vector')
main.cpp:9: error: two or more data types in declaration of 'set'

I don't really see the problem... Here's the code:

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

struct vector
{
double x, y, z;
}

Missing semicolon. Happens all the time.

<snip>
 
T

TB

Martin Jørgensen skrev:
Hi,

I get this using g++:

main.cpp:9: error: new types may not be defined in a return type
main.cpp:9: note: (perhaps a semicolon is missing after the definition
of 'vector')
main.cpp:9: error: two or more data types in declaration of 'set'

I don't really see the problem... Here's the code:

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

struct vector
{
double x, y, z;
}

"main.cpp:9: note: (perhaps a semicolon is missing ..."

struct vector
{
double x, y, z;
};

<snip>
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

osmium said:
:





Missing semicolon. Happens all the time.

<snip>

Thanks a lot, both of you... I was focusing on lines 9+ but I should
have looked at line 7. Guess I'll become better with more experience...


Best regards / Med venlig hilsen
Martin Jørgensen
 
B

Ben Pope

Martin said:
Thanks a lot, both of you... I was focusing on lines 9+ but I should
have looked at line 7. Guess I'll become better with more experience...

The error is usually before the compiler spots it. Hence you should be
looking at 9 and before. Also, it does tell you exactly what the error is!

It's sometimes hard to decrypt error messages, but you get the hang of
it with practice.

Ben Pope
 
J

Jim Langston

Martin Jørgensen said:
Hi,

I get this using g++:

main.cpp:9: error: new types may not be defined in a return type
main.cpp:9: note: (perhaps a semicolon is missing after the definition of
'vector')
main.cpp:9: error: two or more data types in declaration of 'set'

I don't really see the problem... Here's the code:

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

struct vector
{
double x, y, z;
}
....

You've alredy gotten the answer, no ; after the }, but let me say that your
combination of
using namespace std:
and naming a structure/class vector is an accident waiting to happen.

If you include in this program some header that happens to include <vector>
(such as a class that uses vectors) you're gonna start getting compile
errors because of the name collision of ::vector and std::vector.
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Jim Langston wrote:
-snip-
If you include in this program some header that happens to include <vector>
(such as a class that uses vectors) you're gonna start getting compile
errors because of the name collision of ::vector and std::vector.

Ok, I haven't really learned about std::vector, but I understand that I
should just call vector -> uservector or something... Thanks for commenting.


Best regards / Med venlig hilsen
Martin Jørgensen
 
B

Ben Pope

Martin said:
Jim Langston wrote:
-snip-


Ok, I haven't really learned about std::vector, but I understand that I
should just call vector -> uservector or something... Thanks for
commenting.

If you want to call it a vector, call it a vector.

Don't use "using namespace std", and then do this:

namespace martin {
class vector { /* */ };
}

now, you can do either:
martin::vector m_vec;
std::vector s_vec

And everybody knows what you're talking about.

Ben Pope
 
D

Daniel T.

Ben Pope said:
If you want to call it a vector, call it a vector.

Don't use "using namespace std", and then do this:

namespace martin {
class vector { /* */ };
}

now, you can do either:
martin::vector m_vec;
std::vector s_vec

And everybody knows what you're talking about.

I like to use my initials. Something about those TLA's that just works.
:)
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Ben said:
Martin Jørgensen wrote: -snip-

If you want to call it a vector, call it a vector.

Don't use "using namespace std", and then do this:

namespace martin {
class vector { /* */ };
}

Damn... I'm too unexperienced to understand that :)

Let me get it explained a bit more slowly :)

The program I posted was like this:
--------
#include <iostream>
using namespace std;

struct vector
{ ... }

void set(vector & v, double x1, double y1, double z1)
{ .... }

void scale(vector & v, double k)
{ ... }

double inner(vector a, vector b)
{ ... }

void print(vector v)
{ ...}

int main()
{ .... }
-----

Are you saying that is the same thing as:

namespace martin {
class vector { /* All of the above code I posted */ };
}

???

As you can see, I'm pretty unexperienced, but I learn new things every
day now :)
now, you can do either:
martin::vector m_vec;
std::vector s_vec

And everybody knows what you're talking about.

So std::vector is the member function of the "std".... (is it called
std-class?)

And martin::vector is the code I posted?

So my main function could look something like the following?:

int main()
{
martin::vector v1;
martin::vector v2;
martin::set(v1, 1.1, 2.2, 3.3);
v2=v1;
martin::scale(v2, 2);
martin::print(v1); martin::print(v2);
cout << "Inner product = " << martin::inner(v1, v2);
}

?

But I guess I'll have to put my code inside a class-environment, then?


Best regards / Med venlig hilsen
Martin Jørgensen
 
R

roberts.noah

Martin said:
Damn... I'm too unexperienced to understand that :)

The structure of the sentance leads to confusion. He is saying to take
out the line that says, "using namespace std," and then to place your
vector structure into a namespace called "martin".
So std::vector is the member function of the "std".... (is it called
std-class?)

std is a namespace that contains most of the stuff in the standard
library. std::vector is the vector class in the standard library.
int main()
{
martin::vector v1;
martin::vector v2;
martin::set(v1, 1.1, 2.2, 3.3);
v2=v1;
martin::scale(v2, 2);
martin::print(v1); martin::print(v2);
cout << "Inner product = " << martin::inner(v1, v2);
}

?

But I guess I'll have to put my code inside a class-environment, then?

namespace, not class.

The point is to separate your implementation of "vector" from the one
in the standard library.
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

The structure of the sentance leads to confusion. He is saying to take
out the line that says, "using namespace std," and then to place your
vector structure into a namespace called "martin".




std is a namespace that contains most of the stuff in the standard
library. std::vector is the vector class in the standard library.

Hmmm... So a namespace is the thing before the "::"... But the thing
before "::" can also be a class, right (or wrong)?
namespace, not class.

Ok, but I don't know how to do that. Can somebody show me an example -
just copy paste the whole original code from my vector program and
modify as required so I can copy/paste it to my .cpp-file...
The point is to separate your implementation of "vector" from the one
in the standard library.

Yep, got that. Just don't know how exactly...


Best regards / Med venlig hilsen
Martin Jørgensen
 
G

Gavin Deane

Martin said:
(e-mail address removed) wrote:


Ok, but I don't know how to do that. Can somebody show me an example -
just copy paste the whole original code from my vector program and
modify as required so I can copy/paste it to my .cpp-file...

Try this. I've sprinkled comments through the code to help explain.

#include <iostream>
// No more using namespace std;

// Create a new namespace called martin and put the
// definition of your vector structure in it
namespace martin
{
struct vector
{
double x, y, z;
}; // Your original problem was missing this semicolon
}
// The fully qualified name of your structure is martin::vector
// vector is the name of the struct
// martin is the name of the namespace within which vector is defined

// Now when you want to refer to your vector type
// in the code, use its full name
void set(martin::vector & v, double x1, double y1, double z1)
{
v.x = x1; v.y = y1; v.z = z1;
}

void scale(martin::vector & v, double k)
{
v.x *= k; v.y *=k; v.z *=k;
}

double inner(martin::vector a, martin::vector b)
{
return a.x * b.x + a. y * b.y + a.z * b.z;
}

void print(martin::vector v)
{
// vector is a name that lives in the namespace called martin.
// To use vector, the fully qualified name martin::vector has been
used.

// In exactly the same way, cout and endl are names that live in
// the namespace called std, so use fully qualified names again.
std::cout << "Vector(" << v.x << ", " << v.y << ", " << v.z << ")"
<< std::endl;
}

int main()
{
martin::vector v1;
martin::vector v2;
set(v1, 1.1, 2.2, 3.3);
v2=v1;
scale(v2, 2);
print(v1); print(v2);
std::cout << "Inner product = " << inner(v1, v2);
}

The standard library vector class lives in the std namespace. You would
need to #include <vector> to get it. It is a somewhat different beast
from your own vector, even though it has the same name. Now your vector
is in your namespace and the standard library's vector is in the
standard library's namespace, you can quite happily use martin::vector
when you need your one and std::vector when you need the standard
library one - all in the same program, with no fear of confusing
yourself, other people reading your code, or the compiler.

Gavin Deane
 
B

Bo Persson

Martin Jørgensen said:
Hmmm... So a namespace is the thing before the "::"... But the thing
before "::" can also be a class, right (or wrong)?

Right. The "::" is called the scope resolution operator. It works with
nested scopes, like classes and namespaces.

Another special case is when there is nothing before the ::. That
refers to something in the global namespace, which has no name.
Ok, but I don't know how to do that. Can somebody show me an
example - just copy paste the whole original code from my vector
program and modify as required so I can copy/paste it to my
.cpp-file...

Someone just did, a few posts up. :)

< namespace martin {
< class vector { /* */ };
< }


The initial problem (as I see it) is the use of "using namespace
std;". This opens up the entire standard library, making the names
visible in your file. This goes against the idea of having a separate
namespace, in the first place. All those names were put away there on
purpose!

When you open up the std namesapce, it saves some typing so you cane
write cout instead of std::cout. But it also introduces a lot of other
names, that confuse both the compiler and the human readers.

Many of us know that there is not only a vector, but also a set and an
inner_product in the standard library. The fact that you have two
vectors and two sets in your code, makes it harder to read.

Also, typing "using namespace std;" just to save typing "std::" before
cout, is actually a net loss in typing, isn't it? :)


Bo Persson
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Gavin said:
Martin Jørgensen wrote: -snip-

when you need your one and std::vector when you need the standard
library one - all in the same program, with no fear of confusing
yourself, other people reading your code, or the compiler.

Nice... Thanks. It even works...


Best regards / Med venlig hilsen
Martin Jørgensen
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Bo said:
Right. The "::" is called the scope resolution operator. It works with
nested scopes, like classes and namespaces.

And struct's too?
Another special case is when there is nothing before the ::. That
refers to something in the global namespace, which has no name.

Hmm. I've never heard about a global namespace, but okay I also haven't
programmed in c++ for more than about 1 month. Which functions are in
the global namespace?

-snip-
Also, typing "using namespace std;" just to save typing "std::" before
cout, is actually a net loss in typing, isn't it? :)

Yeah... But I just copied the code from my instructors slide...


Best regards / Med venlig hilsen
Martin Jørgensen
 
B

Ben Pope

Martin said:
And struct's too?

There is no difference between classes and structs, except the default
access of its members and bases (private vs. public).
Hmm. I've never heard about a global namespace, but okay I also haven't
programmed in c++ for more than about 1 month. Which functions are in
the global namespace?

Everything that is accessible without a scope operator. i.e.,
everything else you've written so far.

When you do:
using namespace std;
At file scope, it brings everything in the std namespace into the global
namespace. When you declare a variable, class, function etc at file
scope, it goes into the global namespace.
Yeah... But I just copied the code from my instructors slide...

:)

Ben Pope
 

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

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top