Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mmmichelle

macrumors newbie
Original poster
Sep 12, 2008
6
0
San Jose
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
 

mmmichelle

macrumors newbie
Original poster
Sep 12, 2008
6
0
San Jose
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:
Code:
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

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Did you include the line:
Code:
using namespace std;

in your files? If not, you need to use:
Code:
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

macrumors newbie
Original poster
Sep 12, 2008
6
0
San Jose
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

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
In case anyone else is having similar problems, here's the gist of the OPs problem:
Stamps.h:
Code:
class Stamps {
  public:
    static int total;
    int quantity, price;
  private:
    static int getTotal();
    static void setTotal();
    void setQuant(int);
    void setPrice(int);
};

Stamps.cpp:
Code:
...
  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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.