ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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.
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
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?
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.
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;
};
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...
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];
}
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).
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.