所以,现在,我可以从sumtwonums.cpp
文件运行我的代码,但是当我尝试从main运行它时,我遇到了很多错误。我已经把这个代码磨了很长时间,我似乎无法得到它。 (我是c ++的初学者,所以我知道我的代码可以大大改进。)
无论如何,这是我尝试从总和运行它时得到的错误。
In function 'main':
16 C:\Users\jozef\Desktop\SYDE 2017\SYDE 121 LABS\Lab702\sum.cpp undefined reference to `input(SumTwoNums&)
17 C:\Users\jozef\Desktop\SYDE 2017\SYDE 121 LABS\Lab702\sum.cpp undefined reference to `input(SumTwoNums&)
18 C:\Users\jozef\Desktop\SYDE 2017\SYDE 121 LABS\Lab702\sum.cpp undefined reference to `print(SumTwoNums&)'
19 C:\Users\jozef\Desktop\SYDE 2017\SYDE 121 LABS\Lab702\sum.cpp undefined reference to `print(SumTwoNums&)'
20 C:\Users\jozef\Desktop\SYDE 2017\SYDE 121 LABS\Lab702\sum.cpp undefined reference to `sum(SumTwoNums&, SumTwoNums&, int*)'
21 C:\Users\jozef\Desktop\SYDE 2017\SYDE 121 LABS\Lab702\sum.cpp undefined reference to `print(int*)'
sumtwonums.cpp
显示如下:
#include <iostream>
#ifndef SUMTWONUMS_H
#define SUMTWONUMS_H
const int LENGTH = 20;
struct SumTwoNums{
public:
int array[LENGTH];
char c_array[LENGTH];
};
void input(SumTwoNums &s);
void sum(SumTwoNums &s1, SumTwoNums &s2, int s[]);
void print (SumTwoNums &s);
void print (int a[]);
#endif
#include "sumtwonums.h"
using namespace std;
void sum(SumTwoNums &s1, SumTwoNums &s2, int s[]){
for (int i = LENGTH-1; i >= 0; i--){
if (s1.array[i] + s2.array[i] + s [i] > 9 && i != LENGTH-1){
s [i] += (s1.array[i] + s2.array[i] - 10);
s [i-1] += 1;
}else if (s1.array[i] + s2.array[i] + s [i]<= 9 && i != LENGTH-1){
s [i] += s1.array[i] + s2.array[i];
}else if (i == LENGTH-1 && s1.array[i] + s2.array[i] + s [i]> 9 ){
s[i] += s1.array[i] + s2.array[i];
s[i-1] += 1;
}else if (i == LENGTH-1 && s1.array[i] + s2.array[i] + s [i] <= 9 ){
s[i] += s1.array[i] + s2.array[i];
}else if (i == LENGTH-1 && s1.array[i] + s2.array[i] + s [i] > 9){///////
s [i] += (s1.array[i] + s2.array[i]); ////Check To see if overflow
cout << "There is an overflow" << endl; ///////
}
}
}
void input(SumTwoNums &s){
int dig = 0;
for (int i = 0; i < LENGTH; i++){
s.array[i] = 0;
}
cout << "Please enter a number:\n";
char next;
int x = LENGTH-1;
cin.get(next);
while (next != '\n' && next != 'E' && x >= 0){
dig++;
s.c_array[x] = next;
cin.get(next);
x--;
}
for (int i = 1; i <= LENGTH; i++){
if (dig != 0) {
s.array[LENGTH - dig] = s.c_array[LENGTH - i] - '0';
dig--;
}
}
}
void print (SumTwoNums &s){
int count = 2;
for (int i = 0; i < LENGTH ; i++){
if (s.array[i] != 0 || ( s.array[i - 1] != 0 && s.array[i] == 0 && s.array[i + 1] != 0)){
cout << s.array[i];
if (count%3==0 && count != 0 && i != LENGTH-1){
cout << ",";
}
}
count++;
}
cout << endl;
}
void print (int a[]){
int count = 2;
for (int i = 0; i < LENGTH ; i++){
if (a[i] != 0 || ( a[i - 1] != 0 && a[i] == 0 && a[i + 1] != 0)){
cout << a[i];
if (count%3==0 && count != 0 && i != LENGTH-1){
cout << ",";
}
}
count++;
}
cout << endl;
}
sum.cpp
显示如下:
#include <iostream>
#include "sumtwonums.h"
using namespace std;
int main() {
SumTwoNums num1;
SumTwoNums num2;
int total [LENGTH];
int rtotal [LENGTH];
for (int i = 0; i < 20; i ++){
total[i] = 0;
rtotal[i] = 0;
}
input(num1);
input(num2);
print(num1);
print(num2);
sum (num1,num2,total);
print(total);
return 0;
}
我知道之前已经问过这个问题,但我找不到一个我理解得很清楚的答案。谢谢。
答案 0 :(得分:0)
你应该将sumtwonums.cpp分成两个文件,以使它们有效。
sumtwonums.h
#include <iostream>
#ifndef SUMTWONUMS_H
#define SUMTWONUMS_H
const int LENGTH = 20;
struct SumTwoNums{
public:
int array[LENGTH];
char c_array[LENGTH];
};
void input(SumTwoNums &s);
void sum(SumTwoNums &s1, SumTwoNums &s2, int s[]);
void print (SumTwoNums &s);
void print (int a[]);
#endif
sumtwonums.cpp
#include "sumtwonums.h"
using namespace std;
void sum(SumTwoNums &s1, SumTwoNums &s2, int s[]){
for (int i = LENGTH-1; i >= 0; i--){
if (s1.array[i] + s2.array[i] + s [i] > 9 && i != LENGTH-1){
s [i] += (s1.array[i] + s2.array[i] - 10);
s [i-1] += 1;
}else if (s1.array[i] + s2.array[i] + s [i]<= 9 && i != LENGTH-1){
s [i] += s1.array[i] + s2.array[i];
}else if (i == LENGTH-1 && s1.array[i] + s2.array[i] + s [i]> 9 ){
s[i] += s1.array[i] + s2.array[i];
s[i-1] += 1;
}else if (i == LENGTH-1 && s1.array[i] + s2.array[i] + s [i] <= 9 ){
s[i] += s1.array[i] + s2.array[i];
}else if (i == LENGTH-1 && s1.array[i] + s2.array[i] + s [i] > 9){///////
s [i] += (s1.array[i] + s2.array[i]); ////Check To see if overflow
cout << "There is an overflow" << endl; ///////
}
}
}
void input(SumTwoNums &s){
int dig = 0;
for (int i = 0; i < LENGTH; i++){
s.array[i] = 0;
}
cout << "Please enter a number:\n";
char next;
int x = LENGTH-1;
cin.get(next);
while (next != '\n' && next != 'E' && x >= 0){
dig++;
s.c_array[x] = next;
cin.get(next);
x--;
}
for (int i = 1; i <= LENGTH; i++){
if (dig != 0) {
s.array[LENGTH - dig] = s.c_array[LENGTH - i] - '0';
dig--;
}
}
}
void print (SumTwoNums &s){
int count = 2;
for (int i = 0; i < LENGTH ; i++){
if (s.array[i] != 0 || ( s.array[i - 1] != 0 && s.array[i] == 0 && s.array[i + 1] != 0)){
cout << s.array[i];
if (count%3==0 && count != 0 && i != LENGTH-1){
cout << ",";
}
}
count++;
}
cout << endl;
}
void print (int a[]){
int count = 2;
for (int i = 0; i < LENGTH ; i++){
if (a[i] != 0 || ( a[i - 1] != 0 && a[i] == 0 && a[i + 1] != 0)){
cout << a[i];
if (count%3==0 && count != 0 && i != LENGTH-1){
cout << ",";
}
}
count++;
}
cout << endl;
}