簡體   English   中英

我在這個 class 中做錯了什么?

[英]What am I doing wrong in this class?

鏈接.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;
}

header

#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) 在 function 中引用的“class Number __cdecl operator+(class Number &,ZA2F2ED4F8EBC2CBB4C21A29DC40”中引用)" (??H@YA?AVNumber@@AAV0@0@Z)

沒有函數定義:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM