Error

Hello,

My vector program won`t compile:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
 #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 {new int[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 using namespace 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.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

#include <vector>
#include <iostream>


using namespace std;

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 << '}';
}  






int main()


{ 

   const size_t amount = 10;
   vector<vector<double>>* p = new vector<vector<double>>(amount);
   delete p;

   print_vector(std::cout, vector, amount)
}
   

  




Something like this using the STL vector?

That way I don`t have to declare the vector class.
Have you even tried to compile this?

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.
Now I have made some progress but what`s wrong with my initialization?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

#include <vector>
#include <iostream>


using namespace std;

void print_vector(std::ostream& os, const vector<int>& v)

{

    os<< '{'; 


                for (int i=0; i<v.size(); ++i) {
                os << v[i];
                 if (i != v.size() -1) os << ", ";
                }

    os << '}';
}  






int main()


{ 

   using namespace std;

   
    
   
   vector<int> *v = new vector<int>(10);

   for (int i = 0; i < v->size(); ++i) v[i] = 10 + i;       // initialize elements

   

   print_vector(std::cout, *v);

   delete v;
}
   
Line 40: v is a pointer to a vector. v[i] is a reference to the i'th pointer (not the i'th element). This results in undefined behaviour.

Is there some reason your vector has to be dynamically allocated?
Allocating the vector directly is much simpler:
1
2
3
4
5
6
7
8
int main()
{   vector<int> v (10);

   for (size_t i=0; i < v.size(); ++i)
       v[i] = 10 + i;       // initialize elements 

   print_vector(cout, v);
}


BTW, line 33 is unnecessary. You've already included the namespace at line 6.

> it's a bad idea to name your class the same as the standard library
I disagree. Names should be meaningful, to avoid clashes there are namespaces.

> It's only legal because you haven't used using namespace std
> so std::vector and your class are in different namespaces.
wonderful.
I disagree.

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 using namespace std;[/code as he should have, but that did not allievate the problem.
Topic archived. No new replies allowed.