Тема: Класи
Добрий день. Щойно почав вивчати C++ на ubuntu.
#include <stdio.h>
typedef int i;
class string
{
public:
char* str;
int len;
char* tstr;
char* ttstr;
int init(int n, char* st)
{
if(n>0) {
len=n;
str=new char[len];
}
else return -1;
for (i a; a<n; a++) str[a]=st[a];
return 0;
}
};
class pstr
{
public:
int print(string* str)
{
if(str.len>0) printf("%s", str.str);
else return -1;
return 0;
}
};
int main()
{
string s;
pstr p;
s.init(n, "hello, world!");
p.print(s);
return 0;
}
Програма не компілюється командою
8887.cpp: In member function ‘int pstr::print(string*)’:
8887.cpp:28:11: error: request for member ‘len’ in ‘str’, which is of pointer type ‘string*’ (maybe you meant to use ‘->’ ?)
if(str.len>0) printf("%s", str.str);
^
8887.cpp:28:35: error: request for member ‘str’ in ‘str’, which is of pointer type ‘string*’ (maybe you meant to use ‘->’ ?)
if(str.len>0) printf("%s", str.str);
^
8887.cpp: In function ‘int main()’:
8887.cpp:37:8: error: ‘n’ was not declared in this scope
s.init(n, "hello, world!");
^
8887.cpp:38:10: error: no matching function for call to ‘pstr::print(string&)’
p.print(s);
^
8887.cpp:38:10: note: candidate is:
8887.cpp:26:7: note: int pstr::print(string*)
int print(string* str)
^
8887.cpp:26:7: note: no known conversion for argument 1 from ‘string’ to ‘string*’