So I've been turning my programs into classes and I run into errors.
So this program is supposed to allow the user to open the file and either read or write to it.
I've omitted the read part from it as I want to attempt that on my own if I can get help on the write. Should be a similar problem.
I get these compile errors:
fileclass.cpp:13: error: ISO C++ forbids declaration of ‘choice’ with no type
fileclass.cpp:21: error: ISO C++ forbids declaration of ‘choice’ with no type
fileclass.cpp: In member function ‘int file::choice()’:
fileclass.cpp:36: error: cannot convert ‘std::string’ to ‘int’ for argument ‘1’ to ‘ssize_t read(int, void*, size_t)’
fileclass.cpp: In member function ‘void file::write(std::string)’:
fileclass.cpp:53: error: ‘myfile’ was not declared in this scope
fileclass.cpp:55: error: ‘myfile’ was not declared in this scope
fileclass.cpp:56: error: return-statement with a value, in function returning 'void'
fileclass.cpp: In function ‘int main()’:
fileclass.cpp:80: error: ‘choice’ was not declared in this scope
The source code provided and the compilation posted are not coherent; understandable, due to the exclusion of the read() method.
The errors are self explanatory:
fileclass.cpp: In member function ‘void file::write(std::string)’:
fileclass.cpp:53: error: ‘myfile’ was not declared in this scope
fileclass.cpp:55: error: ‘myfile’ was not declared in this scope
fileclass.cpp:56: error: return-statement with a value, in function returning 'void'
http://www.cplusplus.com/doc/tutorial/files/
Remove the "return 0;" in this method, since it isn't relevant.
Also:
You're operating the "choice" function from the scope of class "file" incorrectly:
(supposedly line 13, during declaration and line 21, during definition)
http://www.cplusplus.com/doc/tutorial/classes/
1 2 3 4 5 6 7 8 9 10 11
class Rectangle {
int width, height;
public:
void set_values (int,int);
int area() {return width*height;}
};
void Rectangle::set_values (int x, int y) {
width = x;
height = y;
}
You would declare the object file in main, and call it's functions with the dot(.) operator:
1 2 3 4 5 6
int main () {
Rectangle rect;
rect.set_values (3,4);
cout << "area: " << rect.area();
return 0;
}