我在这个课上做错了什么?

What am I doing wrong in this class?

本文关键字:错了 什么      更新时间:2023-10-16

我在链接.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 %sn",n1.getVal());
//n1.SwitchBase(10);
//cout << char_to_10(n1.getVal(), n1.GetBase());
//cout << endl;
//n1.SwitchBase(10);
//printf("obiectul nostru are valoarea %sn", n1.getVal());
Number n2("19A", 13);
Number n3 = n1 + n2;    
}

我得到的错误是:

LNK2019未解析的外部符号"公共:__thiscall编号::编号(类编号常量&&("(??@Z$QBV 0Number@@QAE@函数"类号 __cdecl 运算符+(类号 &,类号 &("(??H@YA?AVNumber@@AAV0@0@Z(

没有函数的定义:

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