Home > Net >  C forbids declaration
C forbids declaration

Time:10-17

#define true 1
#define TRUE 1
#define True 1
#define false 0
#define FALSE 0
#define False 0
using namespace std;
class TMList
{
public:
  virtual void add(int data, bool* success) = 0;
  virtual int get(int index, bool* success) = 0;
  virtual int getSize() = 0;
  virtual void insertAt(int index, int data, bool* success) = 0;
  virtual void update(int index, int data, int* success) = 0;
  virtual void removeAt() = 0;
  virtual void clear() = 0;
  TMList()
  {

  }
  TMList(int)
  {

  }
};
class TMArrayList :public TMList
{
private:
  int** ptr;
  int capacity;
  int size;
  bool addRow();
public:
  TMAarrayList()
  {
    this->ptr = new int* [10];
    this->ptr[0] = new int[10];
    this->capacity = 10;
    this->size = 0;
  }

  TMAarrayList(int bufferSize)
  {
    if (bufferSize <= 0)
    {
      this->ptr = new int* [10];
      this->ptr[0] = new int[10];
      this->capacity = 10;
      this->size;
    }
    else
    {
      int rows;
      int numberOfPointers;
      rows = bufferSize / 10;
      if (bufferSize % 10 != 0)rows  ;
      numberOfPointers = rows   (10 - (rows % 10);
      this->ptr = new int* [numberOfPointers];
      for (int e = 0;e < rows;e  )
      {
        this->ptr[e] = new int[10];
      }
      this->capacity = rows * 10;
      this->size = 0;
    }
  }

  void add(int data, bool* success);
  int get(int index, bool* success);
  int getSize();
  void insertAt(int index, int data, bool* success);
  void update(int index, int data, int* success);
  void removeAt();
  void clear();
};

// TMArrayList::TMArrayList()
// {
// this->ptr=new int*[10];
// this->ptr[0]=new int [10];
// this->capacity=10;
// this->size=0;
// }

// TMArrayList::TMArrayList(int bufferSize)
// {
// if(bufferSize<=0)
// {
// this->ptr=new int*[10];
// this->ptr[0]=new int [10];
// this->capacity=10;
// this->size;
// }
// else
// {
// int rows;
// int numberOfPointers;
// rows=bufferSize/10;
// if(bufferSize!=0)rows  ;
// numberOfPointers=rows (10-(rows);
// this->ptr=new int *[numberOfPointers];
// for(int e=0;e<rows;e  )
// {
// this->ptr[e]=new int[10];
// }
// this->capacity=rows*10;
// this->size=0;
// }
// }

bool TMArrayList::addRow()
{
  if (capacity % 100 == 0)
  {
    int numberOfPointers;
    numberOfPointers = this->capacity / 10;
    int** tmp = new int* [numberOfPointers   10];
    if (tmp == NULL)return false;
    for (int e = 0;e < numberOfPointers;e  )
      tmp[e] = this->ptr[e];
    delete[] this->ptr;
    this->ptr = tmp;
  }
  int i = this->capacity / 10;
  this->ptr[i] = new int[10];
  if (this->ptr[i] == NULL)return false;
  this->capacity = this->capacity   10;
  return true;
}




void TMArrayList::add(int data, bool* success)
{
  if (success)*success = false;
  if (this->size == this->capacity)
  {
    if (!addRow())return;
  }
  int rowIndex, columnIndex;
  rowIndex = this->size / 10;
  columnIndex = this->size % 10;
  this->ptr[rowIndex][columnIndex] = data;
  this->size  ;
  if (success)*success = true;
}

int TMArrayList::get(int index, int* success)
{
  if (succes)*success = false;
  if (index < 0 || index >= this->size)return 0;
  int rowIndex = index / 10;
  int columnIndex = index % 10;
  if (success)*success = true;
  return ptr[rowIndex][columnIndex];
}

int TMArrayList::getSize()
{
  return this->size;
}

void TMArrayList::update(int index, int data, int* success)
{
  if (success)*success = false;
  int rowIndex = index / 10;
  int columnIndex = index % 10;
  this->ptr[rowIndex][columnIndex] = data;
  if (success)*success = true;
}

int TMArrayList::removeAt(int index, int* success)
{
  if (success)*success = false;
  if (index < 0 || index >= size)return 0;
  bool succ;
  int data = this->get(index, &succ);
  int j;
  int ep = this->size - 2;
  j = index;
  while (j <= ep)
  {
    this->update(j, this->get(j   1, &succ), &succ);
    j  ;
  }
  this->size--;
  if (success)*success = true;
  return data;
}

void TMArrayList::insertAt(int index, int data, bool* success)
{
  if (success)*success = false;
  if (index<0 || index>this->size)return;
  if (index == this->size)
  {
    this->add(data, success);
    return;
  }
  bool succ;
  int k = this->get(this->size - 1, &succ);
  this->add(k, &succ);
  if (succ == false)return;
  int j;
  j = this->size - 3;
  while (j >= index)
  {
    this->update(j   1, this->get(j, &succ), &succ);
    j--;
  }
  this->update(index, data, &succ);
  if (success)*success = true;
}


int main()
{
  TMArrayList list1(6000);
  bool k;
  for (int x = 100;x <= 123;x  )
  {
    list1.add(x, &k);
  }
  cout << "Size is" << list1.getSize() << endl;
  for (int e = 0;e < list1.getSize();e  )
  {
    cout << list1.getSize(e, &k) << " ";
  }
  cout << endl;
  list1.update(102, 5000, &k);
  if (k)cout << "Data update at index 102" << endl;
  else cout << "unable to update Data at index 102" << endl;

  list1.update(3, 3030, &k);
  if (k)cout << "Data update at index 3" << endl;
  else cout << "unable to update Data at index 3" << endl;

  list1.insertAt(8, 6060, &k);
  if (k)cout << "Data insert at index 8" << endl;
  else cout << "unable to insert Data at index 8" << endl;

  for (int e = 0;e < list1.getSize();e  )
  {
    cout << list1.get(e, &k) << " ";
  }
  cout << endl;

  int u = list1.removeAt(3, &k);
  if (k)cout << u << "remove from index 3" << endl;
  else cout << "unable to remove data from index 3" << endl;

  for (int e = 0;e < list1.getSize();e  )
  {
    cout << list1.get(e, &k) << " ";
  }
  cout << endl;
  return 0;

}
  • I have created a constructor but code is throwing an error
  • I have declared two constructors. One is default constructor another is parameterized constructor.
  • The error is "C forbids declaration of with no type" but as we know that constructor has no return type

CodePudding user response:

There are a few problems:

  1. As mentioned above don't redefine true, false and bool

  2. You had a typo in the name of the TMArrayList-s constructor and destructor (you wrote "TMAarrayList")

  3. You didn't close a '(' in line 60

  4. The line 52 toes nothing. You forgot =...?

  5. You didn't intend the code so it is hard to read

(1 and 5 were mentioned above, 4 is not an error, but i think (without understanding the code) it is a bug)

Edit: There are likely other mistakes I just wrote down the most obvious ones. (Most of them were warnings or errors while compileing)

CodePudding user response:

If you are faced with errors you cannot understand, the reason is often a spelling or a bracketing error.

Because they change the logical structure of the code, misleading the compiler, they can result in surprising and unhelpful error messages. A human can see that when you write TMAarrayList you mean TMArrayList; the compiler cannot and simply sees an unrelated function without a return type, which is forbidden.

Additionally, as Botond already mentioned, you have a parenthesis error. Those can also derail compilers to a degree that they only diagnose nonsense after that. These kinds of errors can be really stubborn.

The general strategy dealing with them (if frowning at the code for a minute doesn't reveal anything) is culling the code until the error disappears. That can easily be done with #if 0 ... #endif pairs. Modern editors helpfully grey the disabled code out. The remaining code fragments will usually not link, and you may even get new error messages; but at some point you will get different ones that make sense, and then you can start adding the code back in, bit by bit.

That is sometimes the only way to deal with such issues.

Two general bits of advice:

  • Having problems finding such errors may indicate that a file is too big or that there are too many levels of nesting. In your example you could put each class declaration in its own file, which is a practice observed in many places I've worked at. The member function code could go in a file separate from the one containing the main function, which is also common. While these things are not really necessary for small projects it is good to observe best practice as a learner.
  • Do not post sloppy code on StackOverflow ;-).
  • Related