Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Help with C++

Name: Anonymous 2006-07-16 14:29

I'm learning how to write a roguelike, and it's pretty much the first real programming project i have ever done. I have studied C++ for a bit at school and on my spare time, but i know only the basics, and i thought that programming a simple game would be good learning. I'm trying to make a character creator where you choose a race and a class, then the program rolls your stats, adding different bonuses based on classes and races, but i run into this error:

100 C:\Dev-Cpp\char.c statement cannot resolve address of overloaded function

I'm trying to write the stats to a simple text file, but the program tells me that. What gives?

Name: Anonymous 2006-07-16 14:49

POAST MOAR SAUCE !!!!!

Name: Anonymous 2006-07-16 14:53

Ok, i know the code sucks major amounts of ass, bear with me:

char.c contains this:

#include <fstream>
#include <iostream>
#include "char.h"

int clear();

int stats();

int main()
{
    character creating;
    cout<<"Creating a new character, choose a race:\n";
    cout<<"1.) Mutant\n2.) Robot\n3.) Human\n";
    cin>>creating.Race;
    clear();
    cout<<"Choose a profession for your character:\n";
    cout<<"1.) Brute\n2.) Gunslinger\n3.) Scientist\n";
    cin>>creating.Class;
    clear();
    cout<<"Choose a name: ";
    cin>>creating.name[256];
    stats();
}

int clear()
{
    system("cls");
}

int stats()
{
    int e = 1;
    int r = 2;
    int t = 3;
    character creating;
    srand((unsigned)time(0));
    if ( creating.Race == e ) //1 is mutant.
    {
         character creating;
         creating.Str = (rand()%5)+(rand()%5)+18;
         creating.Dex = (rand()%5)+(rand()%5)+10;
         creating.Int = (rand()%5)+(rand()%5)+5;
         creating.Tou = (rand()%8)+(rand()%8)+15;
         creating.Sen = (rand()%6)+(rand()%6)+6;
         creating.HP = creating.Str/3 + creating.Tou/2 + (rand()%10);
         creating.SP = creating.Int/2 + creating.Sen/3 + (rand()%10); //sum is 64
    }
    if ( creating.Race == r ) //2 is a space robot, protecting you from the secret of space.
    {
         character creating;
         creating.Str = (rand()%6)+(rand()%6)+6;
         creating.Dex = (rand()%8)+(rand()%8)+15;
         creating.Int = (rand()%5)+(rand()%5)+10;
         creating.Tou = (rand()%5)+(rand()%5)+5;
         creating.Sen = (rand()%5)+(rand()%5)+18; //64 here too
         creating.HP = creating.Str/3 + creating.Tou/2 + (rand()%10);
         creating.SP = creating.Int/2 + creating.Sen/3 + (rand()%10);
    }
    if ( creating.Race == t ) //3 is a MUNDANE HYOOOOOMAAAAN.
    {
         creating.Str = (rand()%5)+(rand()%5)+13;
         creating.Dex = (rand()%5)+(rand()%5)+13;
         creating.Int = (rand()%5)+(rand()%5)+13;
         creating.Tou = (rand()%5)+(rand()%5)+13;
         creating.Sen = (rand()%5)+(rand()%5)+12; //And here too, gotta be balanced mon.
         creating.HP = creating.Str/3 + creating.Tou/2 + (rand()%10);
         creating.SP = creating.Int/2 + creating.Sen/3 + (rand()%10);
    }
    if ( creating.Class == e ) //Brute
    {
         creating.Str++;
         creating.Tou++;
         creating.Int--;
    }
    if ( creating.Class == r ) //Gunslinger
    {
         creating.Sen++;
         creating.Dex++;
         creating.Str--;
    }
    if ( creating.Class == t ) //Scientist
    {
         creating.Int++;
         creating.Tou++;
         creating.Dex--;
    }
    ofstream a_file ( "character.cha" );
    a_file<<"Character data, do not edit\n";
    a_file<<creating.name[256]<<"\n";
    a_file<<creating.Race<<"\n";
    a_file<<creating.Class<<"\n";
    a_file<<creating.Str<<"\n";
    a_file<<creating.Int<<"\n";
    a_file<<creating.Dex<<"\n";
    a_file<<creating.Tou<<"\n";
    a_file<<creating.Sen<<"\n";
    a_file<<creating.HP<<"\n";
    a_file<<creating.SP<<"\n";
    a_file<<"End of file";
    a_file.close;
    cout<<"Creating complete!";
}

char.h contains this:

#include <cstdlib>

using namespace std;

class character
{
      public:
             char name[256];
             int Race;
             char Class;
            
             int Str;
             int Int;
             int Dex;
             int Tou;
             int Sen;
            
             int HP;
             int SP;
             };
            

Name: Anonymous 2006-07-16 15:00

lol... line 100 should be "a_file.close()"

Name: Anonymous 2006-07-16 15:00

Oops. :D

Name: Anonymous 2006-07-16 15:08

Also... this error message demonstrates why operator overloading is somewhat evil. It is looking for a overloaded function for the member access operator to pass close as an argument to it. That and FFS, it's kind of stupid to allow for such a fundamental operator for the language to be overloaded.

Name: Anonymous 2006-07-16 23:39

MODS /B/ IS NOT WORKING PLZ FIX CUZ I LIEK /B/. KTHNKSBAI.

Name: Anonymous 2006-07-17 16:43

replace "char name[256]" with "string name", and thank me for the time it took to read your crappy code.

Name: Anonymous 2006-07-17 20:12 (sage)

>>3
YOUR CODE IS LIKE LIQUID PAIN FLOWING THROUGH MY ARTERIES

Name: Anonymous 2006-07-17 22:22 (sage)

    a_file<<creating.name[256]<<"\n";
you mean creating.name, i think. [256] is the 257th character, which does not exist...

Name: Anonymous 2006-07-18 0:21

Aggggghhhh thats not how you use arrays....
All of that creating.name[256] stuff will just try to input/output one char from the memory address creating.name + 256 like >>10 said, which is outside the bounds of the array.

Just use strings like >>8 said, with getline() to take input, because you aren't skilled enough to deal with inputting to arrays while handling possible buffer overflows, etc.

Name: Anonymous 2006-07-18 1:00

Number 3 is going to be crushed by a wall of code if he doesn't organize his project soon. Please learn how to use multiple source files at least.

Name: Anonymous 2006-07-18 1:39

>>12
If he's having trouble just dealing with input, I don't think his project will ever get far enough to have to worry about organization.

Name: Anonymous 2006-07-18 4:34

>>3
PROTIP: system("cls") won't work on anything except Windows's sorry excuse for a terminal emulator (well, maybe some versions of DOS, but I doubt you'd have to worry about that in your userbase). Observe:

$ cls
-bash: cls: command not found

The world would be a much better place if every programmer remembered that not everyone uses the same operating system/browser/compiler/terminal/editor as they do.

Making data members of classes public is evil. Never do it (unless you're making a collection of data like the char_traits template that basic_string uses, or something else fancy). Write accessor and mutator functions instead. Rule #1 of object-oriented programming is to keep the internal states of all your objects consistent.

Use std::endl instead of newline, because endl flushes the output buffer.

As three or so people have suggested, use the string class; it'll save you countless headaches.

Name: Anonymous 2006-07-18 6:48

>>14

That's asinine! How the fuck am I supposed to share data between different classes?

Name: Anonymous 2006-07-18 8:19

>>15
Message passing.

Name: Anonymous 2006-07-18 11:24

>>15
If you don't use data hiding, every single piece of code that uses that class will know how its data is stored and access the data directly.  If you suddenly need to change how the data is stored in the class, all of the other code will need to be changed too.  If it is a large project, you are fucked.  This was what caused the whole y2k thing.

Name: Anonymous 2006-07-18 11:26

This was what caused the whole y2k thing
lolwhat

Name: Anonymous 2006-07-18 15:16 (sage)

This was what caused the whole y2k thing.
i didn't think OOP and classes and all that shit were even invented when the decisions that led to the y2k problem were made.

Name: Ddigit 2006-07-18 16:31

You also forgot to return 0 at the end of main.

Also, it's better to pass the character object as an argument to the function stat instead of relying on that the object stil exists within the stat scope.

Example on how to share data between classes:


#include <iostream>
using namespace std;

class Cnum{
private:
int a;
public:
void set(int i){a = i;};
int get(){return a;};
};

int main(){
Cnum no_one;
Cnum no_two;

no_one.set(10) //sets the int a to 10 inside the object no_one

no_two.set(no_one.get());// sets no_two's a to no_one's a.

cout<<no_two.get()<<endl;

return 0;
}
This way makes sure that you dont access your class varibles by accident.

Name: Anonymous 2006-07-18 16:50

>>19
You don't need classes for data hiding.  My point was that the date was stored in two characters, and everything that accessed the date directly read these two characters.  So when someone went to fix one of these programs, they couldn't just replace the two characters with an int, they had to change every piece of code that accessed the date.

Name: Ddigit 2006-07-18 16:55

You also forgot to return 0 at the end of main.

Also, it's better to pass the character object as an argument to the function stat instead of relying on that the object stil exists within the stat scope.

Example on how to share data between classes:


#include <iostream>
using namespace std;

class Cnum{
private:
int a;
public:
void set(int i){a = i;};
int get(){return a;};
};

int main(){
Cnum no_one;
Cnum no_two;

no_one.set(10) //sets the int a to 10 inside the object no_one

no_two.set(no_one.get());// sets no_two's a to no_one's a.

cout<<no_two.get()<<endl;

return 0;
}
This way makes sure that you dont access your class varibles by accident.

Name: Ddigit 2006-07-18 16:55

i fail a slow doublepost

Name: Anonymous 2006-07-18 17:03

Just use a language with support for property, so you don't have to write a shitload of getters & setters.

Name: Anonymous 2006-07-18 17:04 (sage)

*properties

(eg. C#, Delphi)

Name: Anonymous 2006-07-18 18:39

i dont get why people try to hide class variables at any cost. of course its wise to hide as much variables as possible, but why writing a shitload of getters and setters instead of simply calling the variable itself.

Name: Anonymous 2006-07-18 18:44

>>24-25

pfft... real men don't need refined syntax sugar like properties.

Name: Anonymous 2006-07-18 19:41

>>26
Read >>17

Name: Anonymous 2006-07-18 20:04

>>26
because fugly syntax is better style.  the only reason you would call a 'setX()' is if you want to set an X value.  the only reason you would call a 'getX()' is if you want to get an X value.  the ugliness forces you to be sure you actually want to set/get an X at the time.

if (someClass.X = 5) { someFunction(); } would compile, but likely not work as expected.  also can be a bitch to debug.  whereas:
if (someClass.getX() = 5) { someFunction(); } would give a compile-time error.

this is a good thing.

Name: Anonymous 2006-07-19 7:12

>>24
Just use a language that's powerful enough to write code for you, then you can tell it to write your getters/setters.

Fix'd.

Name: Anonymous 2006-07-19 13:56

>>29
you can define getX() as "SomeClass & getX() { return x; }" and the expression "getX() = 5" works perfectly.

Name: Anonymous 2006-07-19 14:25

>>31
Returning a reference to a variable in the class defeats the whole point of having data hiding, since then whatever uses the getX() function will know how the data is stored in the class.  If the data type of x changes, everything using getX() would have to be changed.

Name: Anonymous 2006-07-19 14:28

>>31
Oh, and if you were gonna give some bullshit about converting x to the old data type in getX() and then returning that, everything would fuck up because the local variable would be deleted when the function returns.

Name: Anonymous 2006-07-19 18:11

>>32
It's even worse than this; now you can't provide any special functionality in setX(), because using getX()= circumvents it. Your variable is as good as public.

Name: Anonymous 2006-07-19 22:45

>>32
You can declare the reference const, so it can be read but not changed. >>33 is a problem, although it could be circumvented by creating the variable off the heap (but then you have to take care of destroying it sometime).

Name: Anonymous 2006-07-19 23:29

>>35
Doesn't matter if it can be changed it or not, the point is that there is no data hiding that way.  haha, and I don't think it is a good programming practice to expect whatever calls your function to also deal with cleanup of the memory it allocated.

Name: Anonymous 2006-07-20 2:03

>>20,22
Declaring variables private and then using getters and setters to use them is not data hiding.  You risk breaking encapsulation by coding shit like that usually, and in your examples there is no reason why Cnum can't be a struct.

Name: Anonymous 2006-07-20 2:53

>>36
True, although you can have the variable belong to the object and take care of creating and destroying it (basic_string does something like this for c_str() and data()). Basically, the question is, which method is the most efficient? If the value's being accessed constantly, it may be cheaper (especially with compiler optimizations) to create one variable and return references to it.

Name: Anonymous 2006-07-20 2:55

Getters and setters are nice for regulating variable access in multithreaded programs, so threads don't end up reading and writing to the variable at the same time

Name: Anonymous 2006-07-20 7:59

>>39
You write concurrent code in a language that doesn't have built in support for concurrency?

Oy.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List