| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 23914 人关注过本帖
标题:求助:没有与参数列表匹配的构造函数
只看楼主 加入收藏
lzc0585
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2016-3-18
结帖率:0
收藏
已结贴  问题点数:20 回复次数:5 
求助:没有与参数列表匹配的构造函数
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
class fra{
public:
    fra(int s, int m);
    int getx(){ return x; }
    int gety(){ return y; }
    int plus(fra a, fra b);
private:
    int x, y;
    void reduct(int m, int s);
};
fra::fra(int s, int m){
    if (s*m < 0){
        x = -abs(s);
        y = abs(m);
    }
    else{
        x = s;
        y = m;
    }
};
void fra::reduct(int m, int s){
    int max, min, k, t, m1, s1;
    m1 = abs(m);    s1 = abs(s);
    if (m1 >= s1){
        max = m1;
        min = s1;
    }
    else {
        max = s1;
        min = m1;
    }
    k = max%min;
    if (k == 0)
        t = min;
    else
    while (k){
        max = min;
        min = k;
        t = k;
        k = max%min;
    }
    m = m / t;
    s = s / t;
    cout << "Answer:" << s << "/" << m << endl;
    return;
}
int fra::plus(fra a, fra b){
    int m, s;
    if (a.gety() == 0 || b.gety() == 0){
        cout << "ERROR!" << endl;
        return 0;
    }
    m = a.gety()*b.gety();
    s = (a.getx()*b.gety()) + (b.getx()*a.gety());
    reduct(m, s);
    return 0;
}
int main(){
    int p, q,k,l;
    cin >> p >> q >> k >> l;
    fra a(p,q), b(k,l);
    fra plus(a, b);//此处a下边提示错误“没有与参数列表匹配的构造函数"fra::fra"实例,参数类型为:(fra,fra)”
    return 0;
}
搜索更多相关主题的帖子: private include public return 
2016-03-18 01:40
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:5 
你这是乱用对象,画虎不成反类犬。
我教你一个方法:先别急着设计类和接口,先将怎么使用的用例写好,然后根据用例去完成接口。
也就是先写出
int main( void )
{
    fra a, b;
    cin >> a >> b;
    cout << (a+b) << endl;
}
然后根据这段用例去设计 fra 吧。


程序代码:
////////////////////////////////////////////////////////
#include <iostream>
#include <exception>

struct fraction
{
    fraction( int n=0, int d=1 );
    fraction& reduct( void );
private:
    int numerator;
    int denominator;

    friend std::istream& operator>>( std::istream&, fraction& f );
    friend std::ostream& operator<<( std::ostream&, const fraction& f );
    friend fraction operator+( const fraction& lhs, const fraction& rhs );
};

////////////////////////////////////////////////////////
fraction::fraction( int n, int d ) : numerator(n), denominator(d)
{
    if( d == 0 )
        throw std::invalid_argument("divided by zero");
}
fraction& fraction::reduct( void )
{
    int a = numerator;
    int b = denominator;
    while( b != 0 )
    {
        int c = a%b;
        a = b;
        b = c;
    }
    numerator /= a;
    denominator /= a;

    if( denominator < 0 )
    {
        numerator = -numerator;
        denominator = -denominator;
    }
    return *this;
}
std::istream& operator>>( std::istream& is, fraction& f )
{
    int n, d;
    if( is>>n>>d )
        f = fraction(n,d);
    return is;
}
std::ostream& operator<<( std::ostream& os, const fraction& f )
{
    return os << f.numerator << '/' << f.denominator;
}
fraction operator+( const fraction& lhs, const fraction& rhs )
{
    int n = lhs.numerator*rhs.denominator + rhs.numerator*lhs.denominator; // 严谨一些的话,这里应该判断是否抛出std::overflow_error
    int d = lhs.denominator*rhs.denominator;
    return fraction(n,d).reduct();
}
////////////////////////////////////////////////////////
using namespace std;

int main( void )
{
    fraction a, b;

    try
    {
        if( !(cin>>a>>b) )
        {
            cerr << "input error.\n";
            return 1;
        }
    }
    catch( const std::exception& e )
    {
        cerr << e.what() << '\n';
        return 2;
    }

    cout << a << " + " << b << " = " << (a+b) << endl;

    return 0;
}

测试用例
输入 a b c d
输出 input error.

输入 3 2 1 0
输出 divided by zero

输入 -30 -42 165 -195
输出 -30/-42 + 165/-195 = -12/91

2016-03-18 09:28
牧羊人94
Rank: 2
等 级:论坛游民
帖 子:14
专家分:41
注 册:2016-3-17
收藏
得分:5 
fra plus(a, b);
这一句不就是代表声明plus对象吗?应该加上::吧
2016-03-18 09:28
wmf2014
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:216
帖 子:2039
专家分:11273
注 册:2014-12-6
收藏
得分:5 
对c++不熟悉,不过你先定义类fra a(p,q),b(k,l),a,b即为类fra实体,实体化时执行类构造函数,使用int变量(p,q,k,l),接下来你又定义类的实体plus,也执行构造函数,但使用的变量是fra类(a,b),很显然,你并没有在类定义构造函数使用类自身的函数,所以这种行为非法,我猜想你是要用类函数plus,因此使用你前面定义的类实体(a,b)中任意一个都可以完成,即将语句“fra plus(a, b);”修改为“a.plus(a,b);”即可。

能编个毛线衣吗?
2016-03-18 09:35
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:5 
int main(){
    int p, q,k,l;
    cin >> p >> q >> k >> l;
    fra a(p,q), b(k,l);
    fra plus(a, b);//此处a下边提示错误“没有与参数列表匹配的构造函数"fra::fra"实例,参数类型为:(fra,fra)”
    return 0;
}
要想这样用只有把plus写成友元函数,既是写在类的公有成员里面,就只能用 <对象名>.plus()的方式调用了。
2016-03-18 22:08
刘哈哈哈
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2019-11-1
收藏
得分:0 
#include<string>
#include<iostream>
using namespace std;

class teacher{
public:
    teacher(string n,int ag,string s,int t,string add,string ti):name(n),age(ag),sex(s),tel(t),address(add)
    {string title=ti;}
    void display();
    string name;
    int age;
    string sex;
    int tel;
    string address;
private:
    string title;
};

void teacher:: display()
    {
        cout<<"name:"<<name<<endl;
        cout<<"age:"<<age<<endl;
        cout<<"sex:"<<sex<<endl;
        cout<<"telphone:"<<tel<<endl;
        cout<<"sddress:"<<address<<endl;
}

class cadre: public teacher{
public:
    cadre(string n,int ag,string s,int t,string add,string p):teacher(n,ag,s,t,add),post(p){}
    void display()
    {
        cout<<"name:"<<name<<endl;
        cout<<"age:"<<age<<endl;
        cout<<"sex:"<<sex<<endl;
        cout<<"telphone:"<<tel<<endl;
        cout<<"sddress:"<<address<<endl;
    }

private:
    string post;
};
程序还没写完,但是:teacher(n,ag,s,t,add),post(p)报错说没有与参数列表匹配的构造函数teacher::teacher,求大神急救!
2019-11-01 14:35
快速回复:求助:没有与参数列表匹配的构造函数
数据加载中...
 
   



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

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