| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5454 人关注过本帖
标题:使用Vs编译成功,可以运行,但是使用G++编译就各种问题,求大神解答
只看楼主 加入收藏
z1143709608
Rank: 2
等 级:论坛游民
帖 子:23
专家分:20
注 册:2016-3-13
结帖率:60%
收藏
已结贴  问题点数:20 回复次数:9 
使用Vs编译成功,可以运行,但是使用G++编译就各种问题,求大神解答
头文件
程序代码:
#ifndef STONEWT_H_INCLUDED
#define STONEWT_H_INCLUDED
class Stonewt
{
private:
    enum {Lbs_per_Sto=14};
    double stone;
    double pounds;
    double pds_left;
public:
    Stonewt();
    Stonewt(double pds);
    Stonewt(double st,double p_left);
    ~Stonewt();
    void show();
    operator int() const;
    operator double() const;
    Stonewt operator+(double pds) const;
    friend std::ostream & operator<<(std::ostream &io,Stonewt &t);
    friend Stonewt operator+(double pds, const Stonewt &T)
    {
        std::cout<<"调用友元函数"<<std::endl;
        return T+pds;
    }
};

#endif // STONEWT_H_INCLUDED
搜索更多相关主题的帖子: stone stone 
2016-04-03 16:17
z1143709608
Rank: 2
等 级:论坛游民
帖 子:23
专家分:20
注 册:2016-3-13
收藏
得分:0 
程序代码:
#include <iostream>
#include "stonewt.h"
using namespace std;

Stonewt::~Stonewt()
{
    
}

Stonewt::Stonewt()
{
    stone=pounds=pds_left=0;
}

Stonewt::Stonewt(double pds)
{
    cout<<"调用了一个参数的构造函数"<<endl;
    stone=int(pds)/Lbs_per_Sto;
    pds_left=int(pds)%Lbs_per_Sto+pds-int(pds);
    pounds=pds;
}

Stonewt::Stonewt(double st, double p_left)
{
    cout<<"调用了2个参数的构造函数"<<endl;
    stone=st;
    pds_left=p_left;
    pounds=st*Lbs_per_Sto+p_left;
}

void Stonewt::show()
{
    cout<<pounds<<endl;
}

Stonewt::operator int() const
{
    return int(pounds+0.5);
}

Stonewt::operator double() const
{
    return double(pounds);
}

Stonewt Stonewt::operator+(double pds) const
{
    Stonewt temp;
    temp=pounds+pds;
    return temp;
}
/*
Stonewt operator+(double pds, const Stonewt &p)
{
    return p+pds;
}
*/
ostream & operator<<(ostream &io, Stonewt &p)
{
    cout<<"调用<<重载"<<endl;
    io<<p.pounds<<endl;
    return io;

}
2016-04-03 16:17
z1143709608
Rank: 2
等 级:论坛游民
帖 子:23
专家分:20
注 册:2016-3-13
收藏
得分:0 
程序代码:
#include <iostream>
#include "stonewt.h"

using namespace std;

int main()
{
    Stonewt poppins(9,2.8);
    double p_w = poppins;
    cout<<"p_w="<<p_w<<endl;
    int p_i= poppins;
    cout<<"p_i="<<p_i<<endl;
    Stonewt ny=19.8;
    ny.show();
    Stonewt nm=20.1+ny;
    nm.show();
    cout<<nm<<endl;
    system("pause");
    return 0;
}
2016-04-03 16:18
z1143709608
Rank: 2
等 级:论坛游民
帖 子:23
专家分:20
注 册:2016-3-13
收藏
得分:0 
分成以上三个文件,VS中是可以运行的,不知道为什么G++编译就出错了
stonewt.h:19:9: error: ‘ostream’ in namespace ‘std’ does not name a type
  friend std::ostream & operator<<(std::ostream &io,Stonewt &t);
         ^
stonewt.h: In function ‘Stonewt operator+(double, const Stonewt&)’:
stonewt.h:22:3: error: ‘cout’ is not a member of ‘std’
   std::cout<<"调用友元函数"<<std::endl;
   ^
stonewt.h:22:36: error: ‘endl’ is not a member of ‘std’
   std::cout<<"调用友元函数"<<std::endl;
                                    ^
main.cpp: In function ‘int main()’:
main.cpp:18:16: error: ‘system’ was not declared in this scope
  system("pause");
                ^
zyf@ubuntu:~/Desktop/test$ g++ stonewt.h stone1.cpp main.cpp
stonewt.h:19:9: error: ‘ostream’ in namespace ‘std’ does not name a type
  friend std::ostream & operator<<(std::ostream &io,Stonewt &t);
         ^
stonewt.h: In function ‘Stonewt operator+(double, const Stonewt&)’:
stonewt.h:22:3: error: ‘cout’ is not a member of ‘std’
   std::cout<<"调用友元函数"<<std::endl;
   ^
stonewt.h:22:36: error: ‘endl’ is not a member of ‘std’
   std::cout<<"调用友元函数"<<std::endl;
                                    ^
zyf@ubuntu:~/Desktop/test$ g++ stonewt.h
stonewt.h:19:9: error: ‘ostream’ in namespace ‘std’ does not name a type
  friend std::ostream & operator<<(std::ostream &io,Stonewt &t);
         ^
stonewt.h: In function ‘Stonewt operator+(double, const Stonewt&)’:
stonewt.h:22:3: error: ‘cout’ is not a member of ‘std’
   std::cout<<"调用友元函数"<<std::endl;
   ^
stonewt.h:22:36: error: ‘endl’ is not a member of ‘std’
   std::cout<<"调用友元函数"<<std::endl;
                                    ^
zyf@ubuntu:~/Desktop/test$ clear

zyf@ubuntu:~/Desktop/test$ g++ stonewt.h
stonewt.h:19:9: error: ‘ostream’ in namespace ‘std’ does not name a type
  friend std::ostream & operator<<(std::ostream &io,Stonewt &t);
         ^
stonewt.h: In function ‘Stonewt operator+(double, const Stonewt&)’:
stonewt.h:22:3: error: ‘cout’ is not a member of ‘std’
   std::cout<<"调用友元函数"<<std::endl;
   ^
stonewt.h:22:36: error: ‘endl’ is not a member of ‘std’
   std::cout<<"调用友元函数"<<std::endl;
                                    ^
zyf@ubuntu:~/Desktop/test$ g++ -i stonewt.h
g++: error: unrecognized command line option ‘-i’
zyf@ubuntu:~/Desktop/test$ g++ stonewt.h stone1.cpp main.cpp
stonewt.h:19:9: error: ‘ostream’ in namespace ‘std’ does not name a type
  friend std::ostream & operator<<(std::ostream &io,Stonewt &t);
         ^
stonewt.h: In function ‘Stonewt operator+(double, const Stonewt&)’:
stonewt.h:22:3: error: ‘cout’ is not a member of ‘std’
   std::cout<<"调用友元函数"<<std::endl;
   ^
stonewt.h:22:36: error: ‘endl’ is not a member of ‘std’
   std::cout<<"调用友元函数"<<std::endl;
2016-04-03 16:19
z1143709608
Rank: 2
等 级:论坛游民
帖 子:23
专家分:20
注 册:2016-3-13
收藏
得分:0 
求大神解惑,谢谢
2016-04-03 16:20
z1143709608
Rank: 2
等 级:论坛游民
帖 子:23
专家分:20
注 册:2016-3-13
收藏
得分:0 
没人吗?
2016-04-03 16:32
hjx1120
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:李掌柜
等 级:贵宾
威 望:41
帖 子:1314
专家分:6927
注 册:2008-1-3
收藏
得分:0 
可能是你的‘stonewt.h’头文件没有引用<iostream>头文件


2016-04-03 17:53
alice_usnet
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:18
帖 子:370
专家分:2020
注 册:2016-3-7
收藏
得分:2 
~Stonewt();

未佩好剑,转身便已是江湖
2016-04-03 18:27
z1143709608
Rank: 2
等 级:论坛游民
帖 子:23
专家分:20
注 册:2016-3-13
收藏
得分:0 
回复 7楼 hjx1120
我刚刚在头文件中加了#include<iostream> 程序就不报错了,可是为什么呢?使用ostream时, 我加了实std::的
2016-04-03 22:59
hjx1120
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:李掌柜
等 级:贵宾
威 望:41
帖 子:1314
专家分:6927
注 册:2008-1-3
收藏
得分:18 
没有加载支持的头文件,声明名称空间有什么用
简单的例子:
程序代码:
int main()
{
    std::cout << "请叫我大神";
    return 0;   

}
不引用iostream是没法编译通过的
2016-04-03 23:36
快速回复:使用Vs编译成功,可以运行,但是使用G++编译就各种问题,求大神解答
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019198 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved