LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-11-2006, 03:31 PM   #1
itz2000
Member
 
Registered: Jul 2005
Distribution: Fedora fc4, fc7, Mandrake 10.1, mandriva06, suse 9.1, Slackware 10.2, 11.0, 12.0,1,2 (Current)]
Posts: 732

Rep: Reputation: 30
help with C++ Class, very easy question


Like I wrote before, I got very little time to learn and understand C++, so I'm doing as hard as possible when I'm not at work, so if you can help me to make this code compileable I'd thank you alot :]



I've made a class so I can practise it a bit... couldn't find more compiling errors since I don't understand it much...
But I hope you can help me to fix my code...

This code is a Window, that you can change size, print size, print x,y, print a sentance on it, etc.
Code:
#include <iostream>
using namespace std;

class Window {

                public:
                Window(int m_x=0, int m_y=0)     {
                                                        int x,y;
                                                        y = m_y;
                                                        x = m_x;
                                                 }
                ~Window() { x = 0;
                                        y = 0;
                }
                int x,y;
                int size(int x,int y) {
                        return x*y; }



                int changesize(int m_x, int m_y)
                        {
                                        x = m_x;
                                        y = m_y;
                        }
                void message(char* mes[20]=' ') {
                                        cout << mes[20] << endl;
                                                }
                void printsize(int m_x, int m_y) {
                                                cout << "x is " << x << endl;
                                                cout << "y is " << y << endl;

                                        }

};

int main() {
                int typex=0, typey=0;
                Window window(10,10);
                cout << "Window Size Function : size check is : " << Window.size(typex,typey); << endl;
                Window.changesize(15,15);
                cout << "Window Changed Size Function : size check is : " << Window.size(typex,typey); << endl;
                char* s_word[20];
                cout << "Enter a line ";
                cin >> s_word[20];
                cout << endl;
                cout << "Window Message says : " << Window.message(s_word[20]) << endl;
                cout << "Window Printsize (x and y sizes) " << Window.printsize(typex, typey);
                cout << "Calling Destructor";
                Window.~Window;
        }
here's the compiling errors :
Code:
[zuki@localhost C++]$ g++ class.cpp
class.cpp: In function ‘int main()’:
class.cpp:40: error: expected primary-expression before ‘.’ token
class.cpp:40: error: expected primary-expression before ‘<<’ token
class.cpp:41: error: expected unqualified-id before ‘.’ token
class.cpp:42: error: expected primary-expression before ‘.’ token
class.cpp:42: error: expected primary-expression before ‘<<’ token
class.cpp:47: error: expected primary-expression before ‘.’ token
class.cpp:48: error: expected primary-expression before ‘.’ token
class.cpp:50: error: expected unqualified-id before ‘.’ token
Or maybe I didn't do it good at all? I don't know, I'd be happy if you can help me.

Thanks!!!

Last edited by itz2000; 07-11-2006 at 03:32 PM.
 
Old 07-11-2006, 03:38 PM   #2
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
Seems like you mixed up the class and its instance. window is an instance of Window, so what you need is window.changesize(15,15) for example.
 
Old 07-11-2006, 03:46 PM   #3
itz2000
Member
 
Registered: Jul 2005
Distribution: Fedora fc4, fc7, Mandrake 10.1, mandriva06, suse 9.1, Slackware 10.2, 11.0, 12.0,1,2 (Current)]
Posts: 732

Original Poster
Rep: Reputation: 30
thanks but it seems that I messed it up more

Code:
 The NEW CODE 
#include <iostream>
using namespace std;

class Window {

                public:
                Window(int m_x=0, int m_y=0)     {
                                                        int x,y;
                                                        y = m_y;
                                                        x = m_x;
                                                 }
                ~Window() { x = 0;
                                        y = 0;
                }
                int x,y;
                int size(int x,int y) {
                        return x*y; }



                int changesize(int m_x, int m_y)
                        {
                                        x = m_x;
                                        y = m_y;
                        }
                void message(char* mes[20]=' ') {
                                        cout << mes[20] << endl;
                                                }
                void printsize(int m_x, int m_y) {
                                                cout << "x is " << x << endl;
                                                cout << "y is " << y << endl;

                                        }

};

int main() {
                int typex=0, typey=0;
                Window window(10,10);
                cout << "Window Size Function : size check is : " << window.size(typex,typey); << endl;
                window.changesize(15,15);
                cout << "Window Changed Size Function : size check is : " << window.size(typex,typey); << endl;
                char* s_word[20];
                cout << "Enter a line ";
                cin >> s_word[20];
                cout << endl;
                cout << "Window Message says : " << window.message(s_word[20]) << endl;
                cout << "Window Printsize (x and y sizes) " << window.printsize(typex, typey);
                cout << "Calling Destructor";
                window.~Window;
        }

The NEW COMPILING ERROR (Much worse?)

Code:
[zuki@localhost C++]$ g++ class.cpp
class.cpp: In function ‘int main()’:
class.cpp:40: error: expected primary-expression before ‘<<’ token
class.cpp:42: error: expected primary-expression before ‘<<’ token
class.cpp:47: error: no matching function for call to ‘Window::message(char*&)’
class.cpp:26: note: candidates are: void Window::message(char**)
class.cpp:48: error: no match for ‘operator<<’ in ‘std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(& std::cout)), ((const char*)"Window Printsize (x and y sizes) ")) << window. Window::printsize(typex, typey)’
/usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/bits/ostream.tcc:67: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/bits/ostream.tcc:78: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/bits/ostream.tcc:90: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/bits/ostream.tcc:125: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/bits/ostream.tcc:159: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/bits/ostream.tcc:102: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/ostream:178: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/ostream:189: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/ostream:193: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/ostream:204: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/bits/ostream.tcc:183: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/bits/ostream.tcc:218: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/bits/ostream.tcc:242: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/ostream:219: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/bits/ostream.tcc:265: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/bits/ostream.tcc:288: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/bits/ostream.tcc:311: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/ostream:449: note:                 std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/bits/ostream.tcc:509: note:                 std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char) [with _Traits = std::char_traits<char>]
/usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/ostream:460: note:                 std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char) [with _Traits = std::char_traits<char>]
/usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/ostream:465: note:                 std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char) [with _Traits = std::char_traits<char>]
/usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/bits/ostream.tcc:571: note:                 std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/bits/ostream.tcc:616: note:                 std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*) [with _Traits = std::char_traits<char>]
/usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/ostream:499: note:                 std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*) [with _Traits = std::char_traits<char>]
/usr/lib/gcc/i586-mandriva-linux-gnu/4.0.1/../../../../include/c++/4.0.1/ostream:504: note:                 std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*) [with _Traits = std::char_traits<char>]
class.cpp:50: error: statement cannot resolve address of overloaded function
What shall I do now?
Thanks
 
Old 07-11-2006, 05:06 PM   #4
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
Sorry, but there are quite a number of flaws in that code. For example:
  • You want to use instance variables x,y, and you probably also want to use these in the constructor. Then you must not declare the variables in the constructor.
  • What do you want window.size to do with its arguments? Just compute the product and return the result? Without using the instance variables or anything like that?
  • Remove the ";" after "window.size(typex,typey)" twice.
  • You probably want to use "char s_word[20];"
  • window.message and window.printsize don't return anything, so there's nothing you can pass to cout.
  • window.~Window();
... and I'm sure that's not all.
 
Old 07-11-2006, 05:55 PM   #5
itz2000
Member
 
Registered: Jul 2005
Distribution: Fedora fc4, fc7, Mandrake 10.1, mandriva06, suse 9.1, Slackware 10.2, 11.0, 12.0,1,2 (Current)]
Posts: 732

Original Poster
Rep: Reputation: 30
Thanks spirit
Code:
[zuki@localhost C++]$ cat class.cpp
#include <iostream>
using namespace std;

class Window {

                public:
                Window(int m_x=0, int m_y=0)     {
                                                        y = m_y;
                                                        x = m_x;
                                                 }
                ~Window() { x = 0;
                                        y = 0;
                }
                int x,y;
                int size(int x,int y) {
                        return x*y; }



                int changesize(int m_x, int m_y)
                        {
                                        x = m_x;
                                        y = m_y;
                        }
                int message(char mes[20]) {
                                        cout << mes[20] << endl;
                                                }
                int printsize(int m_x, int m_y) {
                                                cout << "x is " << x << endl;
                                                cout << "y is " << y << endl;

                                        }

};

int main() {
                int typex=0, typey=0;
                Window window(10,10);
                cout << "Window Size Function : size check is : " << window.size(typex,typey) << endl;
                window.changesize(15,15);
                cout << "Window Changed Size Function : size check is : " << window.size(typex,typey) << endl;
                char s_word[20];
                cout << "Enter a line ";
                cin >> s_word;
                cout << endl;
                cout << "Window Message says : " << window.message(s_word) << endl;
                cout << "Window Printsize (x and y sizes) " << window.printsize(typex, typey);
                cout << "Calling Destructor";
                window.~Window();
        }
Now, the only thing I need after this compiles, it to make it work correctly like I wanted lol
It's not doing what I wanted

but it does compile, and it's a start.
 
Old 07-11-2006, 06:08 PM   #6
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
Quote:
Originally Posted by itz2000
Thanks spirit
Code:
[zuki@localhost C++]$ cat class.cpp
#include <iostream>
using namespace std;

class Window {

                public:
                Window(int m_x=0, int m_y=0)     {
                                                        y = m_y;
                                                        x = m_x;
                                                 }
                ~Window() { x = 0;
                                        y = 0;
                }
                int x,y;
                int size(int x,int y) {
                        return x*y; }



                int changesize(int m_x, int m_y)
                        {
                                        x = m_x;
                                        y = m_y;
                        }
                int message(char mes[20]) {
                                        cout << mes[20] << endl;
                                                }
                int printsize(int m_x, int m_y) {
                                                cout << "x is " << x << endl;
                                                cout << "y is " << y << endl;

                                        }

};

int main() {
                int typex=0, typey=0;
                Window window(10,10);
                cout << "Window Size Function : size check is : " << window.size(typex,typey) << endl;
                window.changesize(15,15);
                cout << "Window Changed Size Function : size check is : " << window.size(typex,typey) << endl;
                char s_word[20];
                cout << "Enter a line ";
                cin >> s_word;
                cout << endl;
                cout << "Window Message says : " << window.message(s_word) << endl;
                cout << "Window Printsize (x and y sizes) " << window.printsize(typex, typey);
                cout << "Calling Destructor";
                window.~Window();
        }
Now, the only thing I need after this compiles, it to make it work correctly like I wanted lol
It's not doing what I wanted

but it does compile, and it's a start.
did you read "spirit receiver"'s post?
Quote:
Originally Posted by spirit receiver
[*]What do you want window.size to do with its arguments? Just compute the product and return the result? Without using the instance variables or anything like that?
also you want message() to say:
Code:
cout << mes << endl;
also printsize() doesn't do anything with its arguments, it might as well not have them
 
Old 07-12-2006, 04:20 PM   #7
faris10
Member
 
Registered: Nov 2005
Location: High Point,NC
Distribution: SUSE 10.1
Posts: 89

Rep: Reputation: 15
#include <iostream>
using namespace std;

class Window {

public:
Window(int m_x=0, int m_y=0) {
y = m_y;
x = m_x;
}
~Window() { x = 0;
y = 0;
}
int x,y;
int size(int x,int y) {
return x*y; }



int changesize(int m_x, int m_y)
{
x = m_x;
y = m_y;
return 0;
}
int message(char mes[20]) {
cout << mes << endl;
return(0);
}
int printsize(int m_x, int m_y) {
cout << "x is " << x <<
endl;
cout << "y is " << y <<
endl;
return(0);

}

};

int main() {
int typex=0, typey=0;
Window window(10,10);
cout << "Window Size Function : size check is : " <<
window.size(typex,typey) << endl;
window.changesize(15,15);
cout << "Window Changed Size Function size check is:";
cout<<window.size(typex,typey) << endl;
char s_word[20];
cout << "Enter a line ";
cin >> s_word;
cout << endl;
cout << "Window Message says : " <<
window.message(s_word) << endl;
cout << "Window Printsize (x and y sizes) " <<
window.printsize(typex, typey);
return(0);
}

This should work. I dont know if exactly you know what you are doing. But this should make output
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Which C++ editor in Linux has the class view/class browser feature imaginationworks Programming 7 05-21-2006 11:09 PM
Java Question on using your own class k1ll3r_x Programming 2 01-22-2005 10:11 AM
IP class question chadi Linux - Networking 7 10-31-2004 12:50 PM
BlackBox.class & VerifierBug.class virus ??? dalek Linux - Security 4 02-29-2004 08:55 AM
newbie c++ compiling class question true_atlantis Programming 4 02-20-2004 08:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration