473,414 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

error in my vector program...

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
16 4403
"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
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
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
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

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

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

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

"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Victor | last post by:
I'm trying to run this java program, but somehow the program always quit w/o giving any error msg at all. it happenned inside the first case statements. Strangely, after printing happen2, it just...
4
by: GRoll21 | last post by:
Hey this is my first time posting on of these. the program just gets the employee number, then it gets hours, how much they get paid, then figures total. for some reason its not liking my last...
1
by: Avery Fong | last post by:
The following program will result in a compile error when building under Debug but will compile under Release. Why does is work under Release mode but not under Debug This program is developed...
1
by: Raghuram N K | last post by:
Hi, Following program compiles and executes successfully in windows with DevCPP compiler. When I compile the same in Linux with 'g++323' compiler I get following assignment error: cannot...
6
by: Bobrick | last post by:
Hi. Thanks to everyone who replied to my last post, it turns out it wasn't the line where I was trying to treat the variable in question as an array which was the problem, but the line above. ...
5
by: wong_powah | last post by:
#include <vector> #include <iostream> using std::cout; using std::vector; enum {DATASIZE = 20}; typedef unsigned char data_t;
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
9
by: subramanian100in | last post by:
Consider the following program: #include <iostream> #include <string> #include <vector> using namespace std; template<class Tclass Vec : public vector<T> {
2
by: brasewel | last post by:
Hi guys, I'm getting an error i cannot figure. Here is my code Header file ------------------------------------------------------------------------------------------- #ifndef Clock_H #define...
4
by: subramanian100in | last post by:
Suppose the following program is named x.cpp #include <iostream> #include <vector> using namespace std; template <class T> class Vec : public vector<T> {
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.