| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1058 人关注过本帖
标题:令人困惑的内联函数
只看楼主 加入收藏
feng00055
Rank: 2
等 级:论坛游民
帖 子:56
专家分:27
注 册:2009-8-26
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:10 
令人困惑的内联函数
为什么Vector() 和 set(double n1, double n2, char form = 'r')
不能设为内联函数:
//vect.h

#ifndef VECTOR_H_
#define VECTOR_H_
#include <iostream>

class Vector {
private:
    double x;
    double y;
    char mode;
public:
    Vector();
    Vector(double n1,double n2,char form = 'r') {x = n1; y = n2;mode=form;}
    void set(double n1,double n2,char form = 'r');
    ~Vector() {}
    double xval()const { return x; }
    double yval()const { return y; }
    double magval()const;
    double angval()const;
    void polar_mode();
    void vect_mode();
    Vector operator+(const Vector & b)const;
    Vector operator-(const Vector & b)const;
    Vector operator-()const;
    Vector operator*(double n)const;
    friend Vector operator*(double n, const Vector & a);
    friend std::ostream & operator<<(std::ostream & os, const Vector & v);
};

#endif     

//vect.cc

#include <cmath>
#include "vect1.h"

const double Rad_to_deg = 57.2957795130823;
Vector::Vector() {
    x = y = 0.0;
    mode = 'r';
}

void Vector::set(double n1, double n2, char form) {
    x = n1;
    y = n2;
    mode = form;
}   

double Vector::magval()const {
    return sqrt(x*x + y*y);
}

double Vector::angval()const {
    if (x == 0.0 && y == 0.0)
        return 0;
    else
        return atan2(y, x);
}

void Vector::polar_mode() {
    double temp;
    temp = sqrt(x*x + y*y);
    y = angval();
    x = temp;   
    mode = 'p';
}

void Vector::vect_mode() {
    double temp;
    temp = x * cos(y);
    y = x * sin(y);
    x = temp;
    mode = 'r';
}

Vector Vector::operator+(const Vector & b)const {
    return Vector(x+b.x, y+b.y);
}

Vector Vector::operator-(const Vector & b)const {
    return Vector(x-b.x, y-b.y);
}

Vector Vector::operator-()const {
    return Vector(-x, -y);
}

Vector Vector::operator*(double n)const {
    return Vector(n*x, n*y);
}   

Vector operator*(double n, const Vector & a) {
    return Vector(n*a.x, n*a.y);
}

std::ostream & operator<<(std::ostream & os, const Vector & v) {
    if (v.mode == 'r')
        os << " (x, y) = (" << v.x << ", " << v.y << ") ";
    else if (v.mode == 'p')
        os << " (m, a) = (" << v.x << ", " << v.y * Rad_to_deg << ") ";
    else
        os << "Vector object mode is invalid";
    return os;
}   

//randwalk.cc

#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include "vect.h"

const int SIZE = 60;

int main(void)
{
    srand(time(0));
    char filename[SIZE];
    std::ofstream fout;
    Vector step;
    Vector result(0.0, 0.0);
    unsigned long steps = 0;
    double direction;
    double dstep;
    double target;
    std::cout << "Enter data file's name: ";
    std::cin.getline(filename, SIZE);
    fout.open(filename);
    if (!fout.is_open()) {
        std::cout << "Can't open the file " << filename << std::endl
            << "Program terminating.\n";
        exit(EXIT_FAILURE);
    }
   
    std::cout << "Target distance(q to quit): ";
    while (std::cin >> target) {
        std::cout << "step length: ";
        if (!(std::cin >> dstep))
            break;
        while (result.magval() < target) {
            direction = rand() % 360;
            step.set(dstep, direction, 'p');
            step.vect_mode();
            result = result + step;
            fout << steps++ << ": " << result << std::endl;
        }
        fout << "After " << steps << " steps, the subject "
            "has the following location:\n" << result
                << std::endl << "or\n";
        result.polar_mode();
        fout << result << std::endl;
        fout << "Average outward distance per step = "
            << result.magval() / steps << std::endl;
        steps = 0;
        result.set(0.0, 0.0);
        std::cout << "target distance(q to quit): ";
    }
    fout << "bye!\n";
    fout.close();
    return 0;
}     


搜索更多相关主题的帖子: 内联 函数 困惑 
2010-08-14 21:04
东海一鱼
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:48
帖 子:757
专家分:4760
注 册:2009-8-10
收藏
得分:20 
set(double n1, double n2, char form = 'r')完全可以设为内联函数 。
但Vector()不行,因为这个构造函数虽然简单,但语义非常复杂。试想如果一个子类继承了它,子类中
调用父类的构造函数,它被作为一个宏展开,那它究竟构造的是那个对象???

举世而誉之而不加劝,举世而非之而不加沮,定乎内外之分,辩乎荣辱之境,斯已矣。彼其于世未数数然也。
2010-08-14 21:39
zhoufeng1988
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:北京
等 级:贵宾
威 望:27
帖 子:1432
专家分:6329
注 册:2009-5-31
收藏
得分:0 
以下是引用东海一鱼在2010-8-14 21:39:57的发言:

set(double n1, double n2, char form = 'r')完全可以设为内联函数 。
但Vector()不行,因为这个构造函数虽然简单,但语义非常复杂。试想如果一个子类继承了它,子类中
调用父类的构造函数,它被作为一个宏展开,那它究竟构造的是那个对象???
学习。
2010-08-15 10:24
pangding
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:北京
等 级:贵宾
威 望:94
帖 子:6784
专家分:16751
注 册:2008-12-20
收藏
得分:0 
哦?!还真没注意过这个问题,构造函数不能内联吗?有很多很简单的构造函数一般还都是直接写在类里了,这就算内联函数吧?
要是不能内联的话,还真是得注意注意。不过从来没听过这说法,以后有空去查查看。
2010-08-15 15:08
feng00055
Rank: 2
等 级:论坛游民
帖 子:56
专家分:27
注 册:2009-8-26
收藏
得分:0 
奇怪了, 我把set(double n1, double n2, char form = 'r')设为
内联时我的g++怎么报错?
2010-08-15 21:43
feng00055
Rank: 2
等 级:论坛游民
帖 子:56
专家分:27
注 册:2009-8-26
收藏
得分:0 
错误为:undefined reference to `Vector::set(double, double, char)'
只要不设为内联就没事

2010-08-15 22:05
pangding
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:北京
等 级:贵宾
威 望:94
帖 子:6784
专家分:16751
注 册:2008-12-20
收藏
得分:0 
你把设成内联函数之后的代码贴上来看一眼。
2010-08-15 23:44
feng00055
Rank: 2
等 级:论坛游民
帖 子:56
专家分:27
注 册:2009-8-26
收藏
得分:0 
//vect1.h

#ifndef VECTOR_H_
#define VECTOR_H_
#include <iostream>

class Vector {
private:
    double x;
    double y;
    char mode;
public:
    Vector();
    Vector(double n1,double n2,char form = 'r') {x = n1; y = n2;mode=form;}
    void set(double n1, double n2, char form = 'r') {
        x = n1;
        y = n2;
        mode = form;
    }   

    ~Vector() {}
    double xval()const { return x; }
    double yval()const { return y; }
    double magval()const;
    double angval()const;
    void polar_mode();
    void vect_mode();
    Vector operator+(const Vector & b)const;
    Vector operator-(const Vector & b)const;
    Vector operator-()const;
    Vector operator*(double n)const;
    friend Vector operator*(double n, const Vector & a);
    friend std::ostream & operator<<(std::ostream & os, const Vector & v);
};

#endif     

//vect1.cc

#include <cmath>
#include "vect1.h"

const double Rad_to_deg = 57.2957795130823;
Vector::Vector() {
    x = y = 0.0;
    mode = 'r';
}

double Vector::magval()const {
    return sqrt(x*x + y*y);
}

double Vector::angval()const {
    if (x == 0.0 && y == 0.0)
        return 0;
    else
        return atan2(y, x);
}

void Vector::polar_mode() {
    double temp;
    temp = sqrt(x*x + y*y);
    y = angval();
    x = temp;   
    mode = 'p';
}

void Vector::vect_mode() {
    double temp;
    temp = x * cos(y);
    y = x * sin(y);
    x = temp;
    mode = 'r';
}

Vector Vector::operator+(const Vector & b)const {
    return Vector(x+b.x, y+b.y);
}

Vector Vector::operator-(const Vector & b)const {
    return Vector(x-b.x, y-b.y);
}

Vector Vector::operator-()const {
    return Vector(-x, -y);
}

Vector Vector::operator*(double n)const {
    return Vector(n*x, n*y);
}   

Vector operator*(double n, const Vector & a) {
    return Vector(n*a.x, n*a.y);
}

std::ostream & operator<<(std::ostream & os, const Vector & v) {
    if (v.mode == 'r')
        os << " (x, y) = (" << v.x << ", " << v.y << ") ";
    else if (v.mode == 'p')
        os << " (m, a) = (" << v.x << ", " << v.y * Rad_to_deg << ") ";
    else
        os << "Vector object mode is invalid";
    return os;
}     
2010-08-16 09:47
pangding
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:北京
等 级:贵宾
威 望:94
帖 子:6784
专家分:16751
注 册:2008-12-20
收藏
得分:0 
我这 g++ 非常正常的就过了。
2010-08-16 11:51
东海一鱼
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:48
帖 子:757
专家分:4760
注 册:2009-8-10
收藏
得分:0 
咳、咳、咳。最背运的VC6也麻溜的过了。楼主的怎末就不过呢?这是个问题。。。

举世而誉之而不加劝,举世而非之而不加沮,定乎内外之分,辩乎荣辱之境,斯已矣。彼其于世未数数然也。
2010-08-16 13:06
快速回复:令人困惑的内联函数
数据加载中...
 
   



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

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