【问题标题】:What am I doing wrong in this class?我在这堂课上做错了什么?
【发布时间】:2020-08-30 02:36:04
【问题描述】:

链接.cpp.h 文件时遇到问题。

这是我的代码:

cpp

#include "Number.h"
#include <string.h>

Number::Number(const char* value, int baza)
{
    val = new char[strlen(value)+1];
    memcpy(val, value, strlen(value) + 1);
    this->base = baza;
}

Number::Number(char* value, int baza)
{
    val = new char[strlen(value) + 1];
    memcpy(val, value, strlen(value) + 1);
    this->base = baza;
}

Number::~Number()
{
    delete val;
    val = nullptr;
}

Number operator+(Number& i1, Number& i2)
{
    int x = char_to_10(i1.getVal(), i1.GetBase()) + char_to_10(i2.getVal(), i2.GetBase());
    cout << x<<endl;
    int baza_mare = (i1.GetBase() > i2.GetBase() ? i1.GetBase() : i2.GetBase());
    char* p = new char[10];
    p=SchimbaBaza(x, baza_mare);
    Number res(p, baza_mare);
    return res;
}

char* SchimbaBaza(int n, int newBase)
{
    int i = 0; int p = 0; int v[1000];
    while (n)
    {
        v[i] = n % newBase;
        i++;
        p++;
        n = n / newBase;
    }
    int j = 0;
    char* aux = new char[p];
    for (i = 0; i <= p; i++)
        aux[i] = NULL;
    for (i = p-1; i >= 0; i--)
    {
        if (v[i] <= 9)
            aux[j++] = (char)(v[i] + 48);
        else if (v[i] == 10)
            aux[j++] = 'A';
        else if (v[i] == 11)
            aux[j++] = 'B';
        else if (v[i] == 12)
            aux[j++] = 'C';
        else if (v[i] == 13)
            aux[j++] = 'D';
        else if (v[i] == 14)
            aux[j++] = 'E';
        else if (v[i] == 15)
            aux[j++] = 'F';
    }
    return aux;
}

void Number::SwitchBase(int newBase)
{
    int i = 0; int p = 0; int v[1000];
    char aux[100];
    int n = char_to_10(getVal(), GetBase());
    while (n)
    {
        v[i] = n % newBase;
        i++;
        p++;
        n = n / newBase;
    }
    int j = 0;
    for (i = p; i >= 0; i--)
    {
        if (v[i] <= 9)
            aux[j++]= (char)(v[i]+48);
        else if (v[i] == 10)
            aux[j++]= 'A';
        else if (v[i] == 11)
            aux[j++] = 'B';
        else if (v[i] == 12)
            aux[j++] = 'C';
        else if (v[i] == 13)
            aux[j++] = 'D';
        else if (v[i] == 14)
            aux[j++] = 'E';
        else if (v[i] == 15)
            aux[j++] = 'F';
    }

    this->base = newBase;
    j = 0;
    for (j = 1; j <=p; j++)
        cout<<aux[j];
    cout << endl;
    for (j = 0; j <p; j++)
        val[j] = NULL;
    for (j = 0; j < p; j++)
        val[j] = aux[j+1];
}

char* Number::getVal()
{
    return (this->val);
}

int Number::GetDigitsCount()
{
    return (strlen(this->val));
}

int Number::GetBase()
{
    return(this->base);
}

void Number::Print()
{
    printf("Numarul nostru in baza %d este %s.\n", this->base, this->val);
}

int char_to_10(const char* text, int baze)
{
    int x = 0;
    char * s = new char[strlen(text) + 1];
    memcpy(s, text, (strlen(text) + 1));
    for (int i = 0; i < strlen(s); i++)
    {
        if (s[i] >= '0' && s[i] <= '9')
            x = x + (s[i] - '0') * pow(baze, (strlen(s) - i - 1));
        if (s[i] == 'A')
            x = x + 10 * pow(baze, (strlen(s) - i - 1));
        if (s[i] == 'B')
            x = x + 11 * pow(baze, (strlen(s) - i - 1));
        if (s[i] == 'C')
            x = x + 12 * pow(baze, (strlen(s) - i - 1));
        if (s[i] == 'D')
            x = x + 13 * pow(baze, (strlen(s) - i - 1));
        if (s[i] == 'E')
            x = x + 14 * pow(baze, (strlen(s) - i - 1));
        if (s[i] == 'F')
            x = x + 15 * pow(baze, (strlen(s) - i - 1));
    }
    return x;
}

标题

#pragma once
#include <string.h>
#include <iostream>
using namespace std;

class Number
{
    char* val;
    int base;
public:
    Number(const char* value, int baza); // where base is between 2 and 16
    Number(char* value, int baza);
    ~Number();

    friend Number operator +(Number& i1, Number& i2);
    friend bool operator -(const Number& i1, const Number& i2);
    bool operator !();
    bool operator[](int index);
    bool operator>(const Number& i);
    Number(const Number& i);
    Number(const Number&& i);
    bool operator--();
    bool operator--(int value);

    void SwitchBase(int newBase);
    void Print();
    char* getVal();
    int  GetDigitsCount(); // returns the number of digits for the current number
    int  GetBase(); // returns the current base
};

int char_to_10(const char* text, int baze);
char* SchimbaBaza(int n, int newBase);

主要

#include <iostream>
#include "Number.h"
using namespace std;

int main()
{
    Number n1("14A", 12);
    printf("Avem numarul de %d cifre in baza %d.\n", n1.GetDigitsCount(), n1.GetBase());
    n1.Print();
    //n1.~Number();
    //n1.SwitchBase(10);
    //n1.Print();
    printf("obiectul nostru are valoarea %s\n",n1.getVal());
    //n1.SwitchBase(10);
    //cout << char_to_10(n1.getVal(), n1.GetBase());
    //cout << endl;
    //n1.SwitchBase(10);
    //printf("obiectul nostru are valoarea %s\n", n1.getVal());
    Number n2("19A", 13);
    Number n3 = n1 + n2;    
}

我得到的错误是:

LNK2019 未解析的外部符号“public: __thiscall Number::Number(class Number const &&)”(??0Number@@QAE@$$QBV0@@Z) 在函数“class Number __cdecl operator+(class Number &,班级编号 &)" (??H@YA?AVNumber@@AAV0@0@Z)

【问题讨论】:

  • 您的Number(Number const &amp;&amp;) 看起来完全错误。应该是Number(Number&amp;&amp;);,你还没有实现。
  • 你的 operator-const 参数但你的 operator+ 没有这也很奇怪。也是时候使用std::string 而不是new char[...]
  • using namespace std; 通常是一个糟糕的主意。在标题中更糟。
  • operator-() 返回一个布尔值,而operator+() 返回一个数字。为什么会有差异?
  • Number n3 = n1 + n2; 中的 n1 + n2 创建一个临时的。在构造 n3 时,可以将其移动或复制到 n3 中,因此您需要一个复制构造函数和/或移动构造函数。另一个注意事项:如果您像这样创建operator+ 签名:Number operator+(Number i1, const Number&amp; i2); 您可以在函数中执行return i1 += i2; - 如果您从实现operator+= 开始,这通常是一个很好的第一步。

标签: c++ class header


【解决方案1】:

没有函数定义:

Number(const Number& i);
Number(const Number&& i);

【讨论】:

  • 如cmets中所说,Number(const Number&amp;&amp; i)也是错误的。
猜你喜欢
  • 2022-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多