LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   help with C++ Class, very easy question (https://www.linuxquestions.org/questions/programming-9/help-with-c-class-very-easy-question-463054/)

itz2000 07-11-2006 03:31 PM

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!!! :study:

spirit receiver 07-11-2006 03:38 PM

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.

itz2000 07-11-2006 03:46 PM

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

spirit receiver 07-11-2006 05:06 PM

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.

itz2000 07-11-2006 05:55 PM

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.

spooon 07-11-2006 06:08 PM

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

faris10 07-12-2006 04:20 PM

#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


All times are GMT -5. The time now is 07:00 AM.