注册 登录
编程论坛 C++教室

这个电脑也不能运行啊?

蛤蟆001 发布于 2016-09-11 19:30, 2109 次点击
14.
#include <iostream.h>
class Tpoint
{
    int x , y;
pubic:
 Tpoint (int x1,int y1)//构造函数  error C2144: syntax error : missing ')' before type 'int'
    {
        x=x1;y=y1;
    }
    void dispoint()
    {
        cout <<"("<< x << "," << y << ")" << endl;
    }
};
void main()
{
    Tpoint a(12,6), *p=new Tpoint(5,12);
    cout <<"First point=>";
    a.dispoint();

    cout <<"Second point=>";
    p->dispoint();
}
6 回复
#2
yangfrancis2016-09-11 20:29
#include <iostream>
using namespace std;
class Tpoint
{
private:
    int x, y;
pubic:
 Tpoint (int x1,int y1)//构造函数  error C2144: syntax error : missing ')' before type 'int'
    {
        x=x1;y=y1;
    }
    void dispoint()
    {
        cout <<"("<< x << "," << y << ")" << endl;
    }
};
int main()
{
    Tpoint a(12,6);Tpoint *p=new Tpoint(5,12);
    cout <<"First point=>";
    a.dispoint();

    cout <<"Second point=>";
    p->dispoint();
    return 0;
}
#3
rjsp2016-09-12 09:21
pubic 应该是 public。
另外,换本书吧,你的书不但老旧,而且还非常恶心。
<iostream.h>在上个世纪就被C++废除了;“void dispoint()”这作者根本就不会C++呀,根本不理解C++就瞎鸡巴写书害人。

程序代码:
#include <iostream>

class Tpoint
{
public:
    Tpoint( int x, int y ) : x_(x), y_(y)
    {
    }
private:
    int x_, y_;

    friend std::ostream& operator<<( std::ostream&, const Tpoint& );
};

std::ostream& operator<<( std::ostream& os, const Tpoint& tp )
{
    return os << '(' << tp.x_ << ',' << tp.y_ << ')';
}

#include <memory>
using namespace std;

int main( void )
{
    Tpoint a(12,6);
    cout << "1st point => " << a << endl;

    unique_ptr<Tpoint> p( new Tpoint(5,12) );
    cout << "2nd point => " << *p << endl;
}


#4
反脑控20162016-09-12 12:17
#include<iostream>
以前,我们要在文件中使用cin、cout和endl等时,必须用#include命令将iostream.h文件包含进来,因为此文件含有cin、cout和endl等的定义。
ANSI在规范化C++的时候对iostream.h进行了一些修改,比如说把其中的所有东西放进了一个叫std的namespace里(还有许多头文件都被这样修改了)。但是顽固的程序员们就不答应了,因为这意味着他们的老程序都要被修改才能适应新编译器。于是ANSI只好保留了原来的iostream.h,并把新的iostream.h改名为iostream。
现在,在vs2010里已经没有以前的iostream.h文件了,所以要在文件中使用cin、cout和endl等,就必须用#include命令将iostream文件包含进来,即在文件中添加“#include<iostream>”。
#5
反脑控20162016-09-12 12:19
命名空间std
ISO/IEC C++的所有标准库工具都被定义在一个名称为std的命名空间内,所以标准库的每一项,它们都有自己的名称,以及作为限定符的命名空间名称std。例如:名称cout和endl是在标准库中定义的,所以它们的全名是std::cout和std::endl。
语句“using std::cout;”和“using std::endl;”是using声明,它们告诉编译器,您打算在不指定命名空间名称的情况下使用命名空间std中的名称cout和endl。
如果我们自己编写了一个通用类库供别人使用,里面有很多类和函数,那么最好把所有东西都放进一个自己定义的namespace。因为,假设我们的类库里面有一个叫Foo的class,而用户原来的程序也有一个同名的class,那么显然就会产生编译错误。而如果我们把所有东西都放进一个叫MyLib的namspace,那么编译器就知道MyLib::Foo是我们的类,Foo是用户的类,即可正常工作。
std是供本地C++库使用的命名空间的名称。
#6
反脑控20162016-09-12 12:27
版主写的你未必能看的懂,所以我写的如下:
程序代码:
#include <iostream>
using std::cout;
using std::endl;
class Tpoint
{
    int x , y;
public:
    Tpoint (int x1,int y1)
    {
        x=x1;y=y1;
    }
    void dispoint()
    {
        cout <<"("<< x << "," << y << ")" << endl;
    }
};
void main()
{
    Tpoint a(12,6), *p=new Tpoint(5,12);
    cout <<"First point=>";
    a.dispoint();

 
    cout <<"Second point=>";
    p->dispoint();
    delete p;//注意:这句不能少,少了这句内存泄漏.
}

#7
反脑控20162016-09-12 12:31
命名空间的产生
标准库是由大量例程组成的,编写这些例程的目的是为了执行许多常见的任务,如处理输入和输出,以及执行基本的数学计算。由于这些例程以及其他具有名称的事物数量巨大,因此在编写自己的程序时,使用的名称很有可能无意中与标准库中定义的名称之一雷同。为了防止这种情况的发生,C++提出命名空间机制。
1