PDA

View Full Version : need help with separate header and class definition files in xcode 3, using c++




mmmichelle
Sep 12, 2008, 11:50 PM
hi all,
i wrote a simple c++ program in xcode 3, which has one class in which i defined all the member functions in the class declaration, then still in the same file, i wrote the main function which used the class. When i compiled it, it worked fine, but now my prof is asking me to separate it into different files.
so basically i did this:

header file: class definition (with just function prototypes, no body) ( included #ifndef; #define; and #endif)
cpp file: #include "header.h" --here i defined the functions that i declared in the header file, i defined them using this template:
<return type> <ClassName>::<function name> (<parameters>)

cpp file: #include "header.h" --this was the main file that used the class above

this does not compile, and i can't figure out why



lee1210
Sep 13, 2008, 12:01 AM
what is/are the error message(s)?

-Lee

mmmichelle
Sep 13, 2008, 01:46 PM
all of the errors are in the member-defining file and in main. most are errors in the member defining file. here are some examples:

void Stamps: setname(string name)
//X error: variable or field 'setname' declared void
//X error: 'int Stamps::setname' is not a static member of 'class Stamps'
//X error: 'string' was not declared in the scope
{customername=name;}
string Stamps :: getname()
//X error: 'string' does not name a type
{return customername;}
void Stamps :: sethowmany (int bhowmany=0)
//X error: no 'void Stamps ::sethowmany(int)' member function declared in class 'Stamps'

i've included #include <string> in the header file so i don't know why it says the stuff about string, and i don't get why it says that i declared the fn void is an error, i want it to be void.


alot of errors say that whatever member fn is not declared in the class

then for the main, i get that the class 'Stamp' has no member named '<insert all the members i called>' for every error in main.

thanks for any help, i feel like i'm missing something really obvious but i can't figure it out.:(

lee1210
Sep 13, 2008, 04:13 PM
Did you include the line:
using namespace std;

in your files? If not, you need to use:
std::string

instead of string.

I don't know if the single : after Stamps in your setname signature was just a typo here, or if that's actually how it appears in your code. If that is how it appears, correct that.

-Lee

mmmichelle
Sep 14, 2008, 06:20 PM
hey thanks,

The single colon was just a typo in the thread code.
I put using namespace std; in all of the files and that helped to fix some errors, but in the member defining cpp file, it still says that there's "no '<member fn>' member function declared in class 'Stamps' "after all of my functions .

I wonder if i chose the right type of new file when i was starting to break up the code. For the two cpp files, i went to file>new file> under 'c and c++' i chose 'c++ file'. These two files are located in the "source" folder, and the header file has the .h after it so i think it's okay, it's located in the same place as the source file, in the project in xcode 3. Do you think location might be it?

lee1210
Sep 14, 2008, 06:40 PM
It's pretty hard to say. Do you have a function named fn anywhere? I'd say to post the code, but if it is for school that's probably ill-advised. If you'd like to send it to me in a PM I can look.

-Lee

mmmichelle
Sep 14, 2008, 07:25 PM
i feel like an idiot for asking, but how do i PM you?

mmmichelle
Sep 14, 2008, 07:30 PM
post

mmmichelle
Sep 14, 2008, 07:34 PM
post

lee1210
Sep 14, 2008, 07:45 PM
i feel like an idiot for asking, but how do i PM you?

Upper right of the forum.

Private Messages

Left hand column, Send New Message.

Don't bump threads, we'll see things we haven't read in bold when we logged in.

-Lee

The OP wasn't bumping, they needed 5 posts to PM. Oops.

lee1210
Sep 14, 2008, 10:00 PM
In case anyone else is having similar problems, here's the gist of the OPs problem:
Stamps.h:

class Stamps {
public:
static int total;
int quantity, price;
private:
static int getTotal();
static void setTotal();
void setQuant(int);
void setPrice(int);
};


Stamps.cpp:

...
static void setTotal() {
total=quantity*price;
}
static int getTotal() {
return total;
}
...


Hopefully that isn't enough to give away the project for anyone else that stumbles across this.

Here was some of the advice given:
The first issue is that you have a method that you declare as static and you use non-static variables in it. The rule is that you can access static members in non-static methods called on instances, but you can only access non-static instance variables in a non-static context. See which function you're doing this in, and remove the static designation.

The last thing is in your .cpp file, the static designator doesn't mean the same thing as it does in the class declaration in your header file. Once you've declared something static in your header file, you don't need to use it again when declaring the function. You can't have two functions named the same thing in the same class, with one static and one local, so the compiler knows which one it is from the class definition.

There were a few other small syntactical issues, as well, but those were the big issues.

-Lee