ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
[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
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.