We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,520 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

I have no clue how to solve this error.

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; 
}
4
Contributors
5
Replies
10 Hours
Discussion Span
5 Months Ago
Last Updated
6
Views
CHOCHOCHO
Light Poster
30 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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 >>

phorce
Master Poster
738 posts since Jul 2011
Reputation Points: 63
Solved Threads: 91
Skill Endorsements: 16

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

CHOCHOCHO
Light Poster
30 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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)!

phorce
Master Poster
738 posts since Jul 2011
Reputation Points: 63
Solved Threads: 91
Skill Endorsements: 16
#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.

Prysm[VA]
Newbie Poster
21 posts since Dec 2012
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0

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_

ravenous
Practically a Master Poster
681 posts since Jul 2005
Reputation Points: 286
Solved Threads: 111
Skill Endorsements: 9

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1042 seconds using 2.71MB