-1

I want to implement a class that can handle numbers that are arbitrarily large. I know i can use other libraries like BigInteger but i just wanted to implement my own thing as practice.

My header file:

#ifndef INT_H
#define INT_H

//#ifndef vector
#include <vector>

class Int{
private:
    vector<int> v;
public:
    Int();
    Int(int);
    void clear();
    void push_back();
    void resize();
    vector<int>::iterator begin();
    vector<int>::iterator end();
    int size();
    void sum(Int &, Int, Int);
    void sub(Int &, Int, Int);
    void prod(Int &, Int, Int);
    Int operator+(const Int &);
    Int operator-(const Int &);
    Int operator*(const Int &);
    Int operator>(Int &);
    Int operator<(Int &);
    Int operator>=(Int &);
    Int operator<=(Int &);
    int& operator[] (Int);
};

//#endif // vector
#endif // INT_H

The problem is it gives me an error on the first encounter of vector on line 9, namely "expected unqualified-id before '<' token"

Any help would be very appreciated.

Edit: Confused define with include. Now i get vector does not name a type

  • Do you really think it has anything to do with the fact that you're trying to write a big integer class? – juanchopanza Jul 30 '17 at 19:51
  • 3
    Change all instances of `vector` to `std::vector`. – Ryan Bemrose Jul 30 '17 at 19:58
  • ... or write `using namespace std;` – Andrey Nasonov Jul 30 '17 at 20:06
  • 5
    @AndreyNasonov [Please don't do that](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). It would be much better to do `using std::vector;` if you really can't change all instances – Ryan Bemrose Jul 30 '17 at 20:08
  • @RyanBemrose I agree, your option is better. But there is usually no problems with `using namespace ... ` in cpp files. – Andrey Nasonov Jul 30 '17 at 21:13
  • 1
    @AndreyNasonov Please read [this](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) answer to understand what sort of problems may crop up with the `using namespace blah` idiom. – SCCC Jul 30 '17 at 22:02

1 Answers1

2

The vector type from #include <vector> is in the std namespace; since the explicit type of vector<int> is not defined in your code, you need to do one of the following to fix this issue:

  1. Rename all instances of vector<T> to std::vector<T> where T is the type the vector will contain (in your case an int).

or

  1. After #include <vector> you need to add the line using std::vector;. With this using declaration, anywhere an unqualified vector type is encountered, it will use the std::vector type.

Remember, since this class is defined in a header, if you use option 2, then anywhere you #include "Int.h", you're also including a using std::vector; declaration.

A side note to your code: I'm not sure what your full intent with your Int class will be, especially since your class offers member functions that resemble a sequence container, but don't forget your assignment operators (e.g. Int& operator=(std::uint32_t i) ...).

Hope that can help.

txtechhelp
  • 6,625
  • 1
  • 30
  • 39