#include <vector>
#include <iostream>
void print_vector(std::ostream& os, std::vector<int>&v, int n)
{
os<< '{';
for (int i=0; i<n; ++i) {
os << v[i];
if (i != n-1) os << ", ";
}
os << '}';
}
class vector {
int sz;
int * elem;
public:
vector(int s)
:sz{s}, elem {newint[s]}
{
for (int i=0; i<s; ++i) elem[i]=0;
}
~vector()
{ delete[] elem; }
};
void f3(int a);
{
std::vector* p = new vector(a);
vector v(a);
print_vector(std::cout, v, a);
delete[] p;
}
int main()
{
int x=10;
f3(x);
}
Any help woth that I would appreciate, thx
Even though it's legal, it's a bad idea to name your class the same as the standard library. It's only legal because you haven't used usingnamespace std so std::vector and your class are in different namespaces.
To that point, line 57 you call print_vector(). Which type do you think you're passing? std::vector or your class?
The definition of print_vector at line 7 says it accepts a std::vector, but you're trying to pass an instance of your vector class. They're not compatible.
Line 54: you're trying to assign a pointer to an instance of your class to a pointer to a std::vector<>. Again, they're not compatible. Also, std::vector requires a <type> specification.
Line 8 is expecting a vector<int> as the second argument.
What are you trying to pass as the second argument on line 37?
vector in this context is a type name, not a variable.
So you think it's a good idea to use identical names? I agree that names should be meaningful. It's possible to use meaningful names and avoid identical names. e.g if the OP had used Vector for his class, it would have still been meaningful and very clear that he was not intending std::vector. If you look at the OP's original post, it's clear he was confusing his class with std::vector. A situation that probably would have been avoided by the use of Vector. The OP avoided usingnamespace std;[/code as he should have, but that did not allievate the problem.