| View previous topic :: View next topic |
| Author |
Message |
Engineer Expert Cheater
Reputation: 1
Joined: 25 Nov 2007 Posts: 170
|
Posted: Tue Jul 01, 2008 8:50 am Post subject: [I.suck.so.much.at.c++] Need newbie help |
|
|
I don't understand how to apply functions to classes... So I need your help (yhea I know I suck).
Here's a tiny,tiny,tiny script I made so you see how I write it down:
| Code: | #include <iostream.h>
typedef unsigned short ush;
class Dog {
public:
ush age;
ush weight;
void Woof();
}
void Dog::Woof() {
cout << "Oh hi! I'm a dog! Me luv you! Err... I mean.. Woof Woof!\n";
}
int main() {
Dog Nina;
Nina.age = 5;
Nina.Woof();
system("pause");
return 0;
} |
I compile and I get 5 errors all pointing to Woof(), four at line 11, one at line 8.[/code] |
|
| Back to top |
|
 |
Zand Master Cheater
Reputation: 0
Joined: 21 Jul 2006 Posts: 424
|
Posted: Tue Jul 01, 2008 8:57 am Post subject: |
|
|
std::cout?
class Dog {
public:
ush age;
ush weight;
void Woof();
}; |
|
| Back to top |
|
 |
Engineer Expert Cheater
Reputation: 1
Joined: 25 Nov 2007 Posts: 170
|
Posted: Tue Jul 01, 2008 9:07 am Post subject: |
|
|
| Zand wrote: | std::cout?
class Dog {
public:
ush age;
ush weight;
void Woof();
}; |
std::cout makes no diference at all, you just type more...
Also, thanks |
|
| Back to top |
|
 |
Zand Master Cheater
Reputation: 0
Joined: 21 Jul 2006 Posts: 424
|
Posted: Tue Jul 01, 2008 10:24 am Post subject: |
|
|
Compilers don't understand when you just type cout, unless you have a
using namespace std somewhere in front.
Still having errors? |
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Tue Jul 01, 2008 10:55 am Post subject: |
|
|
| Zand wrote: | Compilers don't understand when you just type cout, unless you have a
using namespace std somewhere in front. | They do if you | Code: | | #include <iostream.h> | , but it isn't suggested to include the .h one (anymore..).
@Original poster: your mistake is that you've forgot a ; after the class presentation. Eg. | Code: | class Dog {
void Woof();
}; |
Anyway.. This should work and be a bit more "proper" C++: | Code: | #include <iostream>
typedef unsigned short ush;
class Dog {
public:
ush age;
ush weight;
void Woof();
};
void Dog::Woof() {
std::cout << "Oh hi! I'm a dog! Me luv you! Err... I mean.. Woof Woof!" << std::endl;
}
int main(int argc, char *argv[]) {
Dog Nina;
Nina.age = 5;
Nina.Woof();
std::cin.sync();
std::cin.ignore();
return EXIT_SUCCESS;
} |
PS. I suggest you to use newer tutorials and NOT to use ::system("pause"); |
|
| Back to top |
|
 |
Zand Master Cheater
Reputation: 0
Joined: 21 Jul 2006 Posts: 424
|
Posted: Tue Jul 01, 2008 11:10 am Post subject: |
|
|
| I've never done #include <iostream.h> before. I also never do (using namespace std), I just put all the std::'s. |
|
| Back to top |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Tue Jul 01, 2008 12:09 pm Post subject: |
|
|
| Code: | #include <iostream>
class Dog {
public:
int age;
int weight;
Dog(int Age, int Weight) { age = Age; weight = Weight; }
void Woof(void);
};
void Dog::Woof(void)
{
std::cout << "Oh hi! I'm a dog! Me luv you! Err... I mean.. Woof Woof!\n";
}
int main(void)
{
Dog *Nina = new Dog( 5, 68 );
Nina->Woof();
return 0;
}
|
I've always done classes like that =[ |
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Tue Jul 01, 2008 12:28 pm Post subject: |
|
|
| noz3001 wrote: | | I've always done classes like that =[ | This was very very basic class/C++ stuff. I bet he doesn't know what's for example a pointer :)
| Code: | class Dog {
private:
int m_age, m_weight;
public:
Dog(int Age, int Weight) { m_age = Age; m_weight = Weight; }
void Woof(void);
}; | Information hiding(, missing the setters and getters if they are needed) ;) |
|
| Back to top |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Tue Jul 01, 2008 12:46 pm Post subject: |
|
|
Btw Jani, do you know a way to clear the console without using windows API or system?
Oops, Hijacked Thread. |
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Wed Jul 02, 2008 9:55 am Post subject: |
|
|
| noz3001 wrote: | | Btw Jani, do you know a way to clear the console without using windows API or system? | Getting a bit off topic, but whatever. Nah, I haven't came across any really good way to clear the screen, which I could recommend. But I believe that if you can find some portable Curses library, it might have a way. I haven't personally ever tried coding with curses, and barely ever I've need a (portable) way to clear the screen. It's just nicer when everything is readable from the backlog or whatever it's called.
I think I could pass this question to Wiccaan, but I wouldn't count on him :p
PS. I do know that Curses is for *nix, but I bet someone has (tried to do/) done a portable one.
EDIT: | Wikipedia wrote: | Portability
Although the ncurses library was initially developed under Linux, OpenBSD, FreeBSD, and NetBSD it has been ported to many other ANSI/POSIX UNIX systems, mainly by Thomas Dickey. PDCurses, while not identical to ncurses, uses the same function calls and operates the same way as ncurses does except that PDCurses targets different devices, e.g., console windows for DOS, Win32, OS/2, as well as X11. Porting between the two is not difficult. For example, the roguelike game ADOM was written for Linux and ncurses, later ported to DOS and PDCurses. | You might want to check out PDCurses. Altho, if you're using it only to clear the screen, I guess it wouldn't be clever. |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Fri Jul 04, 2008 3:20 pm Post subject: |
|
|
Clearing the console with API:
| Code: | CONSOLE_SCREEN_BUFFER_INFO pConsoleBufferInfo;
COORD cCoords = {0};
GetConsoleScreenBufferInfo( GetStdHandle(STD_OUTPUT_HANDLE), &pConsoleBufferInfo );
FillConsoleOutputCharacter( GetStdHandle(STD_OUTPUT_HANDLE), 32, pConsoleBufferInfo.dwSize.X*pConsoleBufferInfo.dwSize.Y, cCoords, NULL );
SetConsoleCursorPosition( GetStdHandle(STD_OUTPUT_HANDLE), cCoords ); |
_________________
- Retired. |
|
| Back to top |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Fri Jul 04, 2008 3:47 pm Post subject: |
|
|
| Wiccaan wrote: | Clearing the console with API:
| Code: | CONSOLE_SCREEN_BUFFER_INFO pConsoleBufferInfo;
COORD cCoords = {0};
GetConsoleScreenBufferInfo( GetStdHandle(STD_OUTPUT_HANDLE), &pConsoleBufferInfo );
FillConsoleOutputCharacter( GetStdHandle(STD_OUTPUT_HANDLE), 32, pConsoleBufferInfo.dwSize.X*pConsoleBufferInfo.dwSize.Y, cCoords, NULL );
SetConsoleCursorPosition( GetStdHandle(STD_OUTPUT_HANDLE), cCoords ); |
|
I used to use that way =D. |
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Sat Jul 05, 2008 6:51 am Post subject: |
|
|
| noz3001 wrote: | | Btw Jani, do you know a way to clear the console without using windows API or system? |
| Wiccaan wrote: | | Clearing the console with API: | :P |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
|
| Back to top |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Sat Jul 05, 2008 10:30 am Post subject: |
|
|
| Code: | void Clear(void)
{
std::cout << "\033[2J";
} |
So far that works. But I don't know whether it's good or not. |
|
| Back to top |
|
 |
|