I just started to learning how to use classes and specification files. For some reason i keep getting an error saying `length' undeclared (first use this function)

#include <iostream>
#include <fstream>
#include <string>
#include <cctype>

using namespace std;
const int MAXSIZE = 50;
typedef string ItemType;

class List
{
   public:
         List();
   private:
           int length;
           int currentPos;
           ItemType data[MAXSIZE];

};

//--------------------------------------------------------------------------------------------------------
#include "List.h"

List::List()
{     
      length = 0;   // this is where the error points to
      currentPos = 0; 
}

This is weird. What compiler are you using?

Haven't posted an actual thread here, in a while, but, should this really be a code snippet? your not submitting one, just a question -looks over there >>

dev c++, line 21 and bellow is on a different page.

In Dev-C++ (I'm sure it used to be called DevCpp) all are the files inside a project? In other words, it needs to be linked inside a project, rather than just two seperate files (even if they are in the same folder, and you've inluded the file path).. I think that's how DevCpp works, which is why you're getting this error (i guess)!

#include "List.h"
List::List()
{     
      length = 0;   // this is where the error points to
      currentPos = 0; 
}

Shouldn't you write line 2 with the length and currentPos variables inside it?

#include "List.h"
List::List(int length, int currentPos)

Sorry if that's wrong haha, I'm EXTREMELY new to classes.

Shouldn't you write line 2 with the length and currentPos variables inside it?

No, it's fine the way it is. The variables length and currentPos are member variables of the List class so they are available for use inside all non-static member functions of the List class. In this case, the OP (correctly) wants to ensure that the the member variables are initialised when the default constructor is called. So, if you do

List myList;

You know that the length and current position willbe set to the right value (0 in this case).

As a slight aside, often it's good to have a notation that tells you if a variable is a member variable or just a local variable. Personally, I use the m_ notation. So, I'd have gone for m_length and m_currentPos. Other notations just add a _ to the start/end of the name: _length or length_

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.