我在这个课上做错了什么?
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;
}
页眉
#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);
相关文章:
- #定义c-预处理器常量..我做错了什么
- 努力将整数转换为链表。不知道我在这里做错了什么
- .h 和.cpp文件分离时出错,但仅使用 .h 文件时没有错误.我做错了什么?
- 我的C++线程做错了什么?
- 谁能告诉我我用 getline 做错了什么 (cpp) 格式
- 没有输出的合并排序我做错了什么?
- 我正在尝试使用 while 循环从字符串中删除字母,直到没有字母。我在这里做错了什么?
- 在C++中使用 AKS 素数测试计算双胞胎素数 我做错了什么?
- 我一直试图弄清楚我在这个链表程序中做错了什么
- 我正在尝试学习如何在 c++ 中传递指针,但出现错误:没有用于调用"test"的匹配函数。我做错了什么?
- FFMPEG,C++,内存泄漏,我做错了什么?
- 我做错了什么?反向字符串 C++
- 我在这个课上做错了什么?
- 创建整数的 2D 数组,该数组将使用两个函数用随机数填充矩阵.我做错了什么?
- 在不使用内置库函数的情况下添加字符串,我做错了什么?
- 我做错了什么?GetDiskFreeSpaceExA的功能根本不起作用
- 我在查找和字符串上做错了什么?
- 我正在尝试用 c++ 制作一个日志记录框架,但信息没有传递给记录器的子组件,我做错了什么?
- 我在尝试将多个值push_back向量时出错。我做错了什么?
- 插入排序:我做错了什么?