Second project, some errors (A set as a vector)

This is a discussion on Second project, some errors (A set as a vector) within the C++ Programming forums, part of the General Programming Boards category; Hello, I made my second project with Dev-C++. A set (as a vector). The three files: setl.h Code: #ifndef SETL_H ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    26

    Second project, some errors (A set as a vector)

    Hello,

    I made my second project with Dev-C++. A set (as a vector).

    The three files:

    setl.h

    Code:
    #ifndef SETL_H
    #define SETL_H
    
    #include <vector>
    
    class Set
    {
          private:
                  vector<int> v;
                  int maxNum;
          public:
                  Set();
                  Set(int maxNum);
                  ~Set();
                  void store(int val);
                  bool erase(int val);
                  bool empty();
                  void print();
    };
    
    #endif
    setl.cpp

    Code:
    #include <iostream>
    #include "setl.h"
    #include <vector>
    
    Set::Set()
    {  }
    
    Set::Set(maxNum)
    { 
      v.resize(maxNum); 
    }
    
    Set::~Set() {  }
    
    void Set::store(int val)
    {
         v[maxNum] = val;
         maxNum++;
    }
    
    bool Set::erase(int val)
    {
         int i, k;
         for(i = 0; i < maxNum; i++)
               if(val == v[i]) break;
         if(i == maxNum) return false;
         else
             {
                 for(k = i; k < maxNum; k++)
                       v[k] = v[k + 1];
                 maxNum--;
                 return true;
             }
    }
    
    bool Set::empty()
    {
         if(!v.empty()) return false;
         else return true;
    }
    
    bool Set::print()
    {    
         std::cout << "*** Items***\n";
         int i;
         for(int i = 0; i < maxNum; i++)
                 std::cout << v[i] << " ";
         std::cout << std::endl;
    }
    main.cpp

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <vector>
    #include "setl.h"
    #define MAX 20
    
    
    int main()
    {
        using namespace std;
        int maxItems = MAX;
        Set ins(maxItems);
    
    
        ins.store(5);
        ins.store(77);
        ins.store(0);
        ins.store(111);
    
        ins.print();
    
        ins.erase(111);
    
        ins.print();
    
        if(!ins.empty()) cout << "\nNot empty";
        else cout << "\nThe set is empty";
    
    
        ins.erase(5);
        ins.erase(0);
        ins.erase(77);
    
        if(!ins.empty()) cout << "\nNot empty";
        else cout << "\nThe set is empty";
    
        return 0;
    
    }
    (The whole Dev-C++ project is HERE).

    I have these error messages while compiling:

    Code:
    In file included from main.cpp
    ISO C++ forbids declaration of `vector' with no type
    expected `;' before '<' token
    [Build Error]  [main.o] Error 1
    Can you please help me fixing them?

    Thank you
    Last edited by ferenczi; 01-28-2010 at 09:27 AM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    vector belongs to the std namespace. You should refer to it as std::vector.

    BTW, there are a few errors in the code that I'm sure you will find when you start testing it. I will just give the recommendation to use the initializer list to initialize your class member variables. For example:
    Code:
    Set::Set(maxNumParam) : v(maxNumParam), maxNum(maxNumParam)
    {
    }

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    26
    You are completely right.

    This project was full of small inaccuracies. But now works.

    Next time I'll read better my programs before asking.

    Thank you

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,300
    The Loki library has a "set implemented using a vector" already imlemented within it. It's called assoc_vector, and it's far superrior to what you've already got so far.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. STL's vector V.S. STL's set??
    By meili100 in forum C++ Programming
    Replies: 14
    Last Post: 07-10-2007, 11:13 AM
  2. vector of pointers
    By gamer4life687 in forum C++ Programming
    Replies: 3
    Last Post: 09-26-2005, 10:49 PM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. my vector class..easy dynamic arrays
    By nextus in forum C++ Programming
    Replies: 5
    Last Post: 02-03-2003, 09:14 AM
  5. set, vector & list
    By Luigi in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2002, 09:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21