LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-21-2005, 06:54 PM   #1
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
basic (i think) c++ .h error


im working on a very simple c++ program...
i have a header file, as follows:

Code:
#include <iostream>
#include <string>

#ifndef JI_PhoneHeader
#define JI_PhoneHeader

using namespace std;

struct PhoneEntry
{
       string name;
       long phoneNumber;
};

enum SortingType {name, number};

class PhoneBook
{
      PhoneEntry entries[100];
      SortingType sortBy = name;
  
}

#endif
i get this error:
Quote:
Line 22 : ISO C++ forbids initialization of member `sortBy'
it then says that it will make 'sortBy' static... then gives an error about it being static, lol
this is my first header file, so im sure im doing something basic wrong.

any help appreciated.
 
Old 10-21-2005, 07:00 PM   #2
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
I think you should do the initialization in a constructor, i.e.
Code:
class PhoneBook
{
      PhoneEntry entries[100];
      SortingType sortBy;
public:
      PhoneBook() { sortBy = name; }
}
 
Old 10-21-2005, 07:33 PM   #3
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Original Poster
Rep: Reputation: 60
alright, thanks for the help..
however, i still get an error.. but it doesnt point to the header file.. but i think it still has something to do with it, since my tester file basically has nothing in it TO have an error.
heres the file that uses the header (i simply made it so it could complie the header):
Code:
#include "PhoneHeader.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
}
it says "Line 5 : expected unqualified-id before "using" "
and "Line 5 : expected `,' or `;' before "using" "

edit: im in window$ so i dont actually need to return something, so thats not the problem

Last edited by nadroj; 10-21-2005 at 07:34 PM.
 
Old 10-21-2005, 07:45 PM   #4
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
have you added a semi colon to the end of the class ie
Code:
class PhoneBook
{
      PhoneEntry entries[100];
      SortingType sortBy;
public:
      PhoneBook() { sortBy = name; }
};
also i would include iostream and string after the ifndef, it wont make any diff to this code (edit it will, it will mean you dont have to declare them in the main cpp file) but when you have a header which is included in multiple files it ensures they are not included everytime.

Code:
#ifndef JI_PhoneHeader
#define JI_PhoneHeader

#include <iostream>
#include <string>
actually ansi requires that you return an int from main.

Last edited by dmail; 10-21-2005 at 07:51 PM.
 
Old 10-21-2005, 07:51 PM   #5
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Original Poster
Rep: Reputation: 60
perfect.. thanks.

im not used to c++, and this is my first class and header i have ever made..
i normally use java, and you dont make classes the same way.. and dont need the end-of-statement semicolon after declaring a class.

i see what you mean about the (possibly) duplicate #include statements. thanks
 
Old 10-21-2005, 08:34 PM   #6
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Original Poster
Rep: Reputation: 60
i cant belive i cant figure this out and how i cant apply my java knowledge to do it!

well, heres the code for a class i have declared in a header file:

Code:
class PhoneBook
{
      const int max = 100;
      PhoneEntry entries[];
      int next;
      
      SortingType sortBy;
      
public:
      PhoneBook()
      {
      sortBy = name;
      next = 0;
      }
};
but i get the same error as before.. '19 : ISO C++ forbids initialization of member `max' '
so i remove the ' = 100' and add 'max = 100;' to the phonebook constructor.. then.. obviously it gives error saying i cant set the value for max because its const (of course).
doing it the way just described, but removing the const, works.. but i need it to be const.
any hints?

it seems that you cannot initialize any variable outside of a function?
 
Old 10-21-2005, 08:39 PM   #7
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
for it to be initailised inside the class like you have done it, it would have to be a static const int
see http://www.devx.com/tips/Tip/5602
 
Old 10-21-2005, 08:43 PM   #8
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Original Poster
Rep: Reputation: 60
alright..
i read the tip.
it works, thanks again...untill my next question

looks like i have alot to learn with classes in c++.. even after studying java for afew years and being very comfortable with it.
 
Old 10-21-2005, 10:02 PM   #9
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Original Poster
Rep: Reputation: 60
here i come
ok...

in my header file, i have an enum as follows:
Code:
enum SortingType {name, number};
i also have a simple function in the header to change the 'sorting type':

Code:
void changeOrder(SortingType sort)
      {
           sortBy = sort; // sortBy is of type SortingType
      }
in another program, i have an object of this type of class ('PhoneBook'). i am trying to access the changeOrder function to change the type of sorting used, ie:
Code:
myPhoneBook.changeOrder(SortingType);
where SortingType is obviously some data of that type.
in my testing program i also have a string called name and an integer called number... therefore, i cannot use the enumerated value of 'name' to specify the SortingType, ie:
Code:
myPhoneBook.changeOrder(name);
because it is a string, and doesnt know which 'name' im talking about.

how can i access the enumerated value?

i have tried things such as 'PhoneBook.SortingType.name', 'myPhoneBook.SortingType.name' etc

i also tried just specifying 0 or 1.. because enums are basically conts' with integer values i though.. but it doesnt work.

thanks for any help, again.

Last edited by nadroj; 10-21-2005 at 10:20 PM.
 
Old 10-21-2005, 10:32 PM   #10
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Code:
void changeOrder(SortingType sort)
      {
           sortBy = sort; // sortBy is of type SortingType
      }
this func needs to be part of the class interface

like
Code:
class PhoneBook
{  
public:
      PhoneBook()
     {
      sortBy = name;
      next = 0;
      }
      void changeOrder(SortingType sort){ sortBy = sort; }
private:
      static const int max = 100;
      PhoneEntry entries[];
      int next;
      SortingType sortBy;
};
by having the func implentation in the class definition you are inlining it (which is ok), but normally when you have more complicated funcs there are implemented in the corrisponding .cpp

example:
in the .h

Code:
class PhoneBook
{  
public:
      PhoneBook();
      void changeOrder(SortingType sort);
private:
      static const int max = 100;
      PhoneEntry entries[];
      int next;
      SortingType sortBy;
};
in the .cpp file
Code:
#include "enter_header_name_here.h"

//constructor
PhoneBook::PhoneBook()//note :: is the scope operator
{
       sortBy = name;
      next = 0;
}

//setter for the sort type
PhoneBook::changeOrder(SortingType sort)
{
       sortBy = sort;
}
as a additonal note i declare any varibales which are part of the class as:
m_variable_name;
which makes it nicer to do things like
Code:
 
//setter for the sort type
PhoneBook::changeOrder(SortingType sort)
{
       m_sort = sort;
}
to access the sortBy create a getter func
Code:
class PhoneBook
{  
public:
      PhoneBook();
      void changeOrder(SortingType sort);
      SortingType get_sort() { return sortBy; }
private:
      static const int max = 100;
      PhoneEntry entries[];
      int next;
      SortingType sortBy;
};

Last edited by dmail; 10-21-2005 at 10:38 PM.
 
Old 10-21-2005, 10:39 PM   #11
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Original Poster
Rep: Reputation: 60
first.. thanks alot!

sorry.. i didnt want to paste all of the code, but yes, the function is defined in the public: portion of the class declaration.
would it make a difference if i put the enum declaration inside of the class?

also.. are you saying i have to override (well i guess not override.. but define) the changeOrder function in my actual program/tester? i shouldnt HAVE to do that just to access the values in my enumerated type, correct?

thanks again..

edit:
for example.. the beginning part of my .h file is:
Code:
using namespace std;

struct PhoneEntry
{
       string name;
       long phoneNumber;
};

enum SortingType {name, number};

class PhoneBook
{
      static const int max = 100;
      PhoneEntry entries[max];
      int next;
      // ..... etc etc, public declarations follow...
(if that helps at all :S)

Last edited by nadroj; 10-21-2005 at 10:42 PM.
 
Old 10-21-2005, 10:54 PM   #12
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally posted by nadroj
first.. thanks alot!

sorry.. i didnt want to paste all of the code, but yes, the function is defined in the public: portion of the class declaration.
would it make a difference if i put the enum declaration inside of the class?
yes, as it is the enum is global in the file scope.
Quote:
also.. are you saying i have to override (well i guess not override.. but define) the changeOrder function in my actual program/tester? i shouldnt HAVE to do that just to access the values in my enumerated type, correct?
thanks again..
you dont have to, like i said if its in the header file where the class is then its inlined, but you dont normally put it there(unless its a getter/setter like this is )
Quote:
for example.. the beginning part of my .h file is:
Code:
using namespace std;

struct PhoneEntry
{
       string name;
       long phoneNumber;
};

enum SortingType {name, number};

class PhoneBook
{
      static const int max = 100;
      PhoneEntry entries[max];
      int next;
      // ..... etc etc, public declarations follow...
(if that helps at all :S) [/B]
so using this code if you wanted to find the name field in the PhoneEntry
first call the getter (Phone.get_entry(index) ) and then offset to the string giving
Phone.get_entry(0).name;

Code:
string the_name;
PhoneEntry (Phone);//creates the class
the_name = Phone.get_entry(0).name;//see below
Code:
PhoneEntry PhoneBook::get_entry(int index)
{
      if(index >= max)
      {
             //handle the error here
             ;
      }
      else return entries[index];
}

Last edited by dmail; 10-21-2005 at 11:00 PM.
 
Old 10-21-2005, 11:01 PM   #13
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Original Poster
Rep: Reputation: 60
for my question regarding if it would make a difference if i put the enum declaration inside the class.. i know its global now, and would then only be accessible inside the class (as private) if i put it in there..
i was really asking if putting the enum in there would some how solve the problem i am having?

i wouldnt think so..

but my question is still regarding how, in my tester program, access the values in my enum SortingType (ie name and number).
 
Old 10-21-2005, 11:04 PM   #14
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
using the get_entry func
like
Phone.get_entry(index).name;
Phone.get_entry(index).PhoneNumber;

hmm you confused me here having a string name and an enum

could you clarify what you mean by
Quote:
...access the values in my enum SortingType (ie name and number).
hmm i think it would be easier to do this if you sent me your files so i could have a look dmail00@gmail.com

Last edited by dmail; 10-21-2005 at 11:12 PM.
 
Old 10-21-2005, 11:12 PM   #15
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Original Poster
Rep: Reputation: 60


ok.. that function gets a specific PhoneEntry.. i dont need that.
well, i actually already have a 'find' function. that isnt the quesion though

in my tester program (.cpp) i have the following bits of code:
Code:
//...
PhoneBook pb;

/* ...   some code later, i want to use my display() method (public, declared 
in the PhoneBook class) i have to display a list of all phoneEntries.. the 
display method relies on a member in the PhoneBook class called sortBy. 
this is of type SortingType (an enumerateed type in my .h) it can be 
either 'name' or 'number'. if its name, the list will be displayed 
alphabetically.. if 'number' then it will be displayed in order of ascending 
numbers or whatever.
*/
pb.changeOrder(X);
/* X needs to be of type 'SortingType' (again, the enum defined in my .h).. 
say i wanted to set it to the enumerated value declared 'name'.. what
 would i type in place of X?! just typing 'name' wont work.. i have tried 
typing random things like 'PhoneHeader::SortingType.name'  etc
*/
hope its more clear now

thanks

edit:
ill try and describe it very simply, and without names or anything related to the above information:
i want to access a value for an enumerated type in a file other than the one it is declared in.

hopefully that makes sense...

- in one file (.h) i have an enum (enum SortingType {name, number}; )
- in another file (.cpp), i need to specify a value for something of type 'SortingType'.. say the enumerated value for 'name'..
how do i do that?

Last edited by nadroj; 10-21-2005 at 11:20 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
chinput installation error: Basic: Cannot open font -misc-SimSun-medium-r-normal--16- Niceman2005 Linux - Software 1 11-01-2005 07:34 PM
basic linux programming/compiling question (permissions error) Godsmacker777 Programming 11 03-17-2005 11:35 AM
Basic Commands Not Found Error varunbihani Linux - Newbie 5 01-21-2005 04:57 AM
I need some really really basic help. cordedpoodle Linux - Newbie 6 08-23-2003 02:19 PM
I'm a BASIC chap, looking for some info on BASIC programming CragStar Programming 2 01-21-2001 09:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration