By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,478 Members | 1,374 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,478 IT Pros & Developers. It's quick & easy.

error in my vector program...

P: n/a
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);
}
--------------
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Feb 20 '06 #1
Share this Question
Share on Google+
16 Replies


P: n/a
"Martin Jørgensen" writes:

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>
Feb 20 '06 #2

P: n/a
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>
--
TB @ SWEDEN
Feb 20 '06 #3

P: n/a
osmium wrote:
"Martin Jørgensen" writes:
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>


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

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Feb 20 '06 #4

P: n/a
Martin Jørgensen wrote:
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
--
I'm not just a number. To many, I'm known as a string...
Feb 20 '06 #5

P: n/a

"Martin Jørgensen" <un*********@spam.jay.net> wrote in message
news:s8************@news.tdc.dk...
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.
Feb 21 '06 #6

P: n/a
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

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Feb 21 '06 #7

P: n/a
Martin Jørgensen wrote:
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.


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
--
I'm not just a number. To many, I'm known as a string...
Feb 21 '06 #8

P: n/a
In article <43**********************@taz.nntpserver.com>,
Ben Pope <be***************@gmail.com> wrote:
Martin Jørgensen wrote:
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.


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.
:-)

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 21 '06 #9

P: n/a
Ben Pope wrote:
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

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Feb 21 '06 #10

P: n/a

Martin Jørgensen wrote:
Ben Pope wrote:
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 :-)


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.

Feb 21 '06 #11

P: n/a
ro**********@gmail.com wrote:
-snip-
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.


Hmmm... So a namespace is the thing before the "::"... But the thing
before "::" can also be a class, right (or wrong)?
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.


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

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Feb 22 '06 #12

P: n/a

Martin Jørgensen wrote:
ro**********@gmail.com wrote:


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

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...


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

Feb 22 '06 #13

P: n/a

"Martin Jørgensen" <un*********@spam.jay.net> skrev i meddelandet
news:jr************@news.tdc.dk...
ro**********@gmail.com wrote:
-snip-
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)?


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.
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.


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 point is to separate your implementation of "vector" from the
one
in the standard library.


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
Feb 22 '06 #14

P: n/a
Gavin Deane wrote:
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

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Feb 22 '06 #15

P: n/a
Bo Persson wrote:
"Martin Jørgensen" <un*********@spam.jay.net> skrev i meddelandet
news:jr************@news.tdc.dk...
ro**********@gmail.com wrote:
-snip-
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)?

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

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Feb 22 '06 #16

P: n/a
Martin Jørgensen wrote:
Bo Persson wrote:

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


And struct's too?


There is no difference between classes and structs, except the default
access of its members and bases (private vs. public).
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?


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.
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...


:)

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 22 '06 #17

This discussion thread is closed

Replies have been disabled for this discussion.