Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

½Ð°Ý¦r¦êªº°ÝÃD

0 views
Skip to first unread message

cdc

unread,
Nov 10, 1997, 3:00:00 AM11/10/97
to

我有兩個程式有問題想請教大家

1.
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>

class data
{
public:
char *str;
data()
{
char str1[30];
cin >> str1;
str=str1;
}
};

void print(char *a)
{
cout << a << "\n"; <-- 沒有執行(output 是空的但有執行換行 "\n" )
printf("%s\n",a); <-- 沒有執行(output 是空的但有執行換行 "\n" )
}
main()
{

data a;
cout << a.str << "\n"; <-- 只有這列執行
printf("%s\n",a.str); <-- 沒有執行(output 是空的但有執行換行 "\n" )
print(a.str);

}

2.
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>

class data
{
public:
char str[30];
data()
{
cin >> str;
}
data(char *f)
{
str=f; <--為什麼不能這樣指定,不是pointer to pointer嗎?
}
};

void print(char *a)
{
cout << a << "\n";
printf("%s\n",a);
}
main()
{

data a("rterter");
cout << a.str << "\n";
printf("%s\n",a.str);
print(a.str);

}

這個連編譯都沒過
error message:
b.c: In method ata::data(char *)':
b.c:15: incompatible types in assignment of har *' to har[30]'

我試過這樣改就完全沒問題
data(char *f)
{
strcpy(str,f);
}
可是我想知道原來錯在哪

先謝謝各位的幫忙�
--
※ Origin: 楓橋驛站(bbs.cs.nthu.edu.tw) ◆ From: istsun10.stat.nctu.edu.tw

0 new messages