Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to pass an array of objects as argument

Status
Not open for further replies.

Bloosy

Technical User
Jun 23, 2003
15
I've got a class Supplier and in main program I have declared an array:
Code:
Supplier* suppliers[10];
then I filled it with ten objects.
Now I need to call a supMask() function from the other file, mask.cpp, where I defined it like this:
Code:
void supMask(Supplier* suppliers[]) {
    for(int i=0; i<10;i++) {
        cout<<suppliers[i]->getName()<<endl;
        }
}
and call it from main like this:
Code:
supMask(suppliers[10]);

It compiles ok, but I get link error:
error LNK2001: unresolved external symbol "void __cdecl supMask(class Supplier *)
What am I doing wrong here?
 
The best way to pass an array is to use a vector instead.
Example:
Code:
std::vector<Supplier*> suppliers;
and
Code:
void supMask( std::vector<Supplier*> suppliers )
{
    std::vector<Suppliers*>::iterator it = suppliers.begin();
    std::vector<Suppliers*>::iterator end = suppliers.end();

    for ( ; it != end; ++it )
    {
        cout << it->getName() << endl;
    }
}
 
I have changed my array declaration into
Code:
std::vector<Supplier*> suppliers;
and put a definition as you sad, but I got 27 compile errors, the first being:
error C2039: 'vector' : is not a member of 'std'
and also the last two:
Wholesale.cpp(148) : error C2143: syntax error : missing ';' before '{'
and
Wholesale.cpp(148) : error C2447: missing function header (old-style formal list?)

in that line,(148), is only the opening bracket of the definition of your function.

Is it possible to figure out what's going on here?
Previously, I had declared that array of pointers of Supplier class.
Code:
Supplier* suppliers[10];
and than I created objects from file:
Code:
infile.open("suppliers.txt");
	if(!infile) {cout< < "File couldn't be opened"; exit(1);}
	string product;
	float price;
	for(int i=0; i<5; i++) {
		suppliers[i] = new Supplier();
		infile>>suppliers[i]->name>>suppliers[i]->contact>>suppliers[i]->address>>suppliers[i]->telefone>>product>>price; 
		suppliers[i]->setProduct(product);
		suppliers[i]->setPrice(price);
		products[i] = suppliers[i]->getProduct();
	}
	infile.close();
it all worked untill I decided to use that array in other function, supMask().
Could you please give me directions how can I do this with vector?
Thank you
 
To use a vector you need to #include <vector>
To add something into a vector, use the push_back() method of the vector class.

After all your #include directives, you can also add:
Code:
using namespace std;
which will allow you to type: vector<something> instead of std::vector<something>.

Here is a list of all the members of the vector class:

For the code you posted for example, you can use vectors like this:
Code:
vector<Suppliers*> suppliers;

...

infile.open( "suppliers.txt" );

if ( !infile )
{
    cout << "File couldn't be opened";
    exit( 1 );
}

string product;
float price;
Supplier* supplier = NULL;

for ( int i = 0; i < 5; ++i )
{
    supplier = new Supplier;
    infile >> supplier->name >> supplier->contact >> supplier->address >> supplier->telefone >> product >> price; 
    supplier->setProduct( product );
    supplier->setPrice( price );
    products[i] = supplier->getProduct();
    suppliers.push_back( supplier );
}

infile.close();
A vector acts like an array that automatically grows as big as you need it, instead of having to set a specific size at compile time, or dynamic memory allocations at runtime.
 
Thank you very much, I'm gonna try that
 
Almost there. The only error is in line
Code:
cout << it->getName() << endl;
Wholesale.cpp(162) : error C2227: left of '->getName' must point to class/struct/union

I tried with it.getName() but with same result
 
This code:

supMask(suppliers[10]);

sends one Supplier pointer to the function, the 11th one in the array (which only has 10, causing who what kind of problems). The correct way to call the function would be

supMask(suppliers);

Lee
 
Since the iterator is storing a pointer and the iterator itself uses the -> operator, you need to do this:
Code:
cout << (*it)->getName() << endl;
That way the (*it) returns the Supplier pointer, and -> dereferences the pointer to call the getName() function.
 
This looks a LOT like a homework assignment I had recently in a C++ class I took to brush up on the language.

Lee
 
Yes, when I call function without [], it works! Now I am sure I had tried it before, (cause I tried every combination, even the sensless ones :)).
The vector also works.
Many thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top