BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

vector passed to function

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++)
View previous topic :: View next topic  
Author Message
DKat
Guest





PostPosted: Tue Nov 22, 2005 6:46 pm    Post subject: vector passed to function Reply with quote



I have tried this several different ways and none of them will compile.

void RandIndex(vector<short>, short) ;
void RandIndex(&vector<short>, short) ;
void RandIndex(std::vector<short>, short) ;
and many others that were just plain wrong but I'm trying everything. I
either get an link error (unresolved external) or a cannot convert x to y
error.

Help! please.... (and yes I did a search to find it this has been answered
before).

RandIndex(SoundIndex,NumberOfTrials);
RandIndex(PictureIndex,NumberOfPictures);

void RandIndex(vector<short> &ndx, short num)
{
randomize();
int x,temp,y;
float f;
for (x=0; x < num;++x)
ndx[x] = x;
for (x=0; x < num;++x)
{
f=rand();
f = f/32767;
y = f * num;
temp = ndx[y];
ndx[y] = ndx[x];
ndx[x] = temp;
}
}


Back to top
Chris Uzdavinis (TeamB)
Guest





PostPosted: Tue Nov 22, 2005 6:54 pm    Post subject: Re: vector passed to function Reply with quote



"DKat" <disisdkatspam (AT) hotmail (DOT) com> writes:

Quote:
I have tried this several different ways and none of them will compile.

It's very important that you show the exact error message(s) you
received. At least the relevant ones. Otherwise it's hard to know
what type of problem we're even trying to solve, unless it jumps out
at us.

Quote:

void RandIndex(vector<short>, short) ;


I'm assuming you're using the std namespace, or else this would fail
to compile for not knowing what a vector is. Assuming that, this
should compile, and will pass the argument by value (copying the
vector.)

Quote:
void RandIndex(&vector<short>, short) ;

This is invalid syntax. The ampersand should go after the type, not
before it. Then it will pass the artument by reference. No copy
involved.

Quote:
void RandIndex(std::vector<short>, short) ;

This should compile, and will pass the argument by value (copying the
vector.)


Quote:
and many others that were just plain wrong but I'm trying everything. I
either get an link error (unresolved external) or a cannot convert x to y
error.

What are you WANTING it to do? Pass by reference or by value?

An unresolved external occurs when you say there is a function that
doesn't exist, and you call it.

Quote:
Help! please.... (and yes I did a search to find it this has been answered
before).

RandIndex(SoundIndex,NumberOfTrials);
RandIndex(PictureIndex,NumberOfPictures);

What are these two lines? Your invocations? What are the types of
the arguments?

It's also VERY important to show code that is complete enough that we
can cut-and-paste it and get the same results you're asking about.
Seeing out-of-context code is better than nothing, but it's not a good
example.

Right now I know you're calling RandIndex with something, and you're
declaring the function somehow, but you're implementing it
differently. Sorry that's not very useful.


Quote:
void RandIndex(vector<short> &ndx, short num)
{
....
}

If that is your implementation, then your prototype should match it
exactly.

--
Chris (TeamB);

Back to top
DKat
Guest





PostPosted: Tue Nov 22, 2005 7:57 pm    Post subject: Re: vector passed to function Reply with quote



I have never passed a vector to a function and what I have tried just
doesn't work...............

All I'm trying to do is have a function that can use arguments - one of
which is a vector (it ise allocated before the function is called). The
first argument is the array (vector<short>) and the second argument is the
size of the array. The actually array itself must be changed so I do not
want to pass by value (which shouldn't be happening with an array anyway?).

I gave several different attempts that I had made at declaring the function.
None of them work.

I either get "[Linker Error] Unresloved external
'TMainForm::RandIndex(std::vector<short,std::allocator&, short)'
reference from c:]......path..filename.obj

Prototype = void RandIndex(vector<short> &, short);
function = void RandIndex(vector<short> &ndx, short num){}

Prototype = void RandIndex(vector<short>, short);
Or

[C++ Error] Control.cpp(315): E2034 Cannot convert
'vector<short,allocator' to 'vector<short,allocator *'
[C++ Error] Control.cpp(315): E2340 Type mismatch in parameter 1 (wanted
'vector<short,allocator *', got 'vector<short,allocator')

if I try to vary how I declare the prototype
"Chris Uzdavinis (TeamB)" <chris (AT) uzdavinis (DOT) com> wrote

Quote:
"DKat" <disisdkatspam (AT) hotmail (DOT) com> writes:

I have tried this several different ways and none of them will compile.

It's very important that you show the exact error message(s) you
received. At least the relevant ones. Otherwise it's hard to know
what type of problem we're even trying to solve, unless it jumps out
at us.


void RandIndex(vector<short>, short) ;


I'm assuming you're using the std namespace, or else this would fail
to compile for not knowing what a vector is. Assuming that, this
should compile, and will pass the argument by value (copying the
vector.)

void RandIndex(&vector<short>, short) ;

This is invalid syntax. The ampersand should go after the type, not
before it. Then it will pass the artument by reference. No copy
involved.

void RandIndex(std::vector<short>, short) ;

This should compile, and will pass the argument by value (copying the
vector.)


and many others that were just plain wrong but I'm trying everything. I
either get an link error (unresolved external) or a cannot convert x to y
error.

What are you WANTING it to do? Pass by reference or by value?

An unresolved external occurs when you say there is a function that
doesn't exist, and you call it.

Help! please.... (and yes I did a search to find it this has been
answered
before).

RandIndex(SoundIndex,NumberOfTrials);
RandIndex(PictureIndex,NumberOfPictures);

What are these two lines? Your invocations? What are the types of
the arguments?

It's also VERY important to show code that is complete enough that we
can cut-and-paste it and get the same results you're asking about.
Seeing out-of-context code is better than nothing, but it's not a good
example.

Right now I know you're calling RandIndex with something, and you're
declaring the function somehow, but you're implementing it
differently. Sorry that's not very useful.


void RandIndex(vector<short> &ndx, short num)
{
...
}

If that is your implementation, then your prototype should match it
exactly.

--
Chris (TeamB);



Back to top
DKat
Guest





PostPosted: Tue Nov 22, 2005 8:04 pm    Post subject: Re: vector passed to function Reply with quote

Did I really proof read this???? sorryI have never passed a vector to a
function and what I have tried just
doesn't work...............

All I'm trying to do is have a function that can use arguments - one of
which is a vector (it is allocated before the function is called). The
first argument is the array (vector<short>) and the second argument is the
size of the array. The passed array itself needs to change so I do not
want to pass by value (which shouldn't be happening with an array anyway?).

I made several different attempts at declaring the function and prototype.
None of them work.

I either get "[Linker Error] Unresloved external
'TMainForm::RandIndex(std::vector<short,std::allocator&, short)'
reference from c:]......path..filename.obj

Prototype = void RandIndex(vector<short> &, short);
function = void RandIndex(vector<short> &ndx, short num){}
Or
Prototype = void RandIndex(vector<short>, short);

or the error

[C++ Error] Control.cpp(315): E2034 Cannot convert
'vector<short,allocator' to 'vector<short,allocator *'
[C++ Error] Control.cpp(315): E2340 Type mismatch in parameter 1 (wanted
'vector<short,allocator *', got 'vector<short,allocator')

Prototype = void RandIndex(vector<short> *, short);
function = void RandIndex(vector<short> *ndx, short num){}


Back to top
Dennis Jones
Guest





PostPosted: Tue Nov 22, 2005 8:23 pm    Post subject: Re: vector passed to function Reply with quote


"DKat" <disisdkatspam (AT) hotmail (DOT) com> wrote

Quote:
Did I really proof read this???? sorryI have never passed a vector to a
function and what I have tried just
doesn't work...............

All I'm trying to do is have a function that can use arguments - one of
which is a vector (it is allocated before the function is called). The
first argument is the array (vector<short>) and the second argument is the
size of the array. The passed array itself needs to change so I do not
want to pass by value (which shouldn't be happening with an array
anyway?).

I made several different attempts at declaring the function and prototype.
None of them work.

I either get "[Linker Error] Unresloved external
'TMainForm::RandIndex(std::vector<short,std::allocator&, short)'
reference from c:]......path..filename.obj

Prototype = void RandIndex(vector<short> &, short);
function = void RandIndex(vector<short> &ndx, short num){}
Or
Prototype = void RandIndex(vector<short>, short);

or the error

[C++ Error] Control.cpp(315): E2034 Cannot convert
'vector<short,allocator' to 'vector<short,allocator *'
[C++ Error] Control.cpp(315): E2340 Type mismatch in parameter 1 (wanted
'vector<short,allocator *', got 'vector<short,allocator ')

Prototype = void RandIndex(vector function = void RandIndex(vector<short> *ndx, short num){}

Honestly, I'm having a little trouble making sense of your examples, but
here is an example that will work:

#include <vector>
short int RandIndex( std::vector<short> &TheVector )
{
// return one of the values from in the vector:
return TheVector[ 5 ];
}

// example usage
std::vector<short> MyVector;

// put some data in the vector:
for ( short int i=0; i<10; ++i )
{
MyVector.push_back( i*2 );
}

// example call
short int temp = RandIndex( MyVector ); // would return the value 10 (5 *
2)

- Dennis



Back to top
Old Wolf
Guest





PostPosted: Tue Nov 22, 2005 9:09 pm    Post subject: Re: vector passed to function Reply with quote


Chris Uzdavinis (TeamB) wrote:

Quote:
"DKat" <disisdkatspam (AT) hotmail (DOT) com> writes:

I either get "[Linker Error] Unresloved external
'TMainForm::RandIndex(std::vector<short,std::allocator&, short)'
reference from c:]......path..filename.obj

Prototype = void RandIndex(vector<short> &, short);
function = void RandIndex(vector<short> &ndx, short num){}

Yes, these match, and should "work".

Perhaps he declared RandIndex inside a class, but then defined a
free function RandIndex. That would cause the error message he pasted.

To "DKat": can you copy and paste your exact program that isn't
working? If your "Prototype" is inside TMainForm and your
"function" is outside it, you have to refer to the function as
"TMainForm::RandIndex", eg:

// in header file
class TMainForm
{
void foo( vector<short> &v );
}

// in cpp file
void TMainForm::foo( vector<short> &v ) {}



Back to top
Chris Uzdavinis (TeamB)
Guest





PostPosted: Tue Nov 22, 2005 9:16 pm    Post subject: Re: vector passed to function Reply with quote

"DKat" <disisdkatspam (AT) hotmail (DOT) com> writes:

Quote:
I have never passed a vector to a function and what I have tried just
doesn't work...............

All I'm trying to do is have a function that can use arguments - one of
which is a vector (it ise allocated before the function is called). The
first argument is the array (vector<short>) and the second argument is the
size of the array. The actually array itself must be changed so I do not
want to pass by value (which shouldn't be happening with an array anyway?).

A vector is not an array, though there is a lot of similarity at a
conceptual level. A vector is just a class. If you pass it by value,
you get a copy, period. An array cannot be passed by value because it
implicitly is transformed into a pointer.

Also, vectors inherently know how big they are, so you don't need to
provide the size to a function that recieves the vector.

Quote:
I gave several different attempts that I had made at declaring the function.
None of them work.

I either get "[Linker Error] Unresloved external
'TMainForm::RandIndex(std::vector<short,std::allocator&, short)'
reference from c:]......path..filename.obj

This is a problem of your definition (not your declaration) not
matching what you're calling.


Quote:

Prototype = void RandIndex(vector<short> &, short);
function = void RandIndex(vector<short> &ndx, short num){}

Yes, these match, and should "work".

Quote:
[C++ Error] Control.cpp(315): E2034 Cannot convert
'vector<short,allocator' to 'vector<short,allocator *'
[C++ Error] Control.cpp(315): E2340 Type mismatch in parameter 1 (wanted
'vector<short,allocator *', got 'vector<short,allocator')

It looks like the compiler is seeing a function that takes a POINTER
to a vector, but you're passing a reference to one.

[rest omitted]

--
Chris (TeamB);

Back to top
Andrue Cope [TeamB]
Guest





PostPosted: Wed Nov 23, 2005 9:19 am    Post subject: Re: vector passed to function Reply with quote

DKat wrote:

Quote:
I have never passed a vector to a function and what I have tried just
doesn't work...............

Rule number one working with the STL: use a typedef :)

typedef std::vector<short> TVectorShort;

void func( TVectorShort & Wibble );

It'll save on typing and improve readability. Actually I'd rarely just
use 'TVectorShort' but would instead use 'TVectorRandomShorts' or
something equally specific.

Quote:
void RandIndex(vector<short> &ndx, short num)
{
randomize();
int x,temp,y;
float f;
for (x=0; x < num;++x)
ndx[x] = x;

How do you know that 'ndx' has enough elements in it to perform this
assignment? If the function is supposed to assume that the caller will
have preinitialised it (nasty idea) then add some verification ie;

void RandIndex(vector {
if( ndx.size()!=num )
..do something

or else size the vector on entry:

void RandIndex(vector<short> &ndx, short num)
{
ndx.resize( num );
--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html

Back to top
DKat
Guest





PostPosted: Fri Nov 25, 2005 6:39 pm    Post subject: Re: vector passed to function Reply with quote

The error was (I am very embarrassed to say)
void MainForm::RandIndex(vector<short> &ndx, short num)
vs
void RandIndex(vector<short> &ndx, short num)
(oops! I just forgot since I program both in C and builder C++ and then
compounded the error by trying to change how I was declaring the arguments
which I had done properly to begin with.....)

However, I was given a lot of helpful advice and taught things I need to
know in any case so thank you all very much.


Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.