| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1058 人关注过本帖
标题:令人困惑的内联函数
取消只看楼主 加入收藏
feng00055
Rank: 2
等 级:论坛游民
帖 子:56
专家分:27
注 册:2009-8-26
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
令人困惑的内联函数
为什么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
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
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
feng00055
Rank: 2
等 级:论坛游民
帖 子:56
专家分:27
注 册:2009-8-26
收藏
得分:0 
这么多人都过了,看来是我这的问题,
我是在Win7上装的VM,虚拟的Ubuntu 10.04
2010-08-16 14:57
快速回复:令人困惑的内联函数
数据加载中...
 
   



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

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