2017-11-02 3 views
0

So, jetzt kann ich meinen Code aus der sumtwonums.cpp Datei ausführen, aber ich bekomme eine Menge Fehler, wenn ich versuche, es von der Hauptsache zu starten. Ich habe diesen Code für eine sehr lange Zeit gemahlen und ich kann es nicht ganz scheinen. (Ich bin ein Anfänger bei C++, also weiß ich, dass mein Code stark verbessert werden könnte).
Wie auch immer, hier ist der Fehler, den ich bekomme, wenn ich versuche, es aus der Summe auszuführen.C++ - Undefiniert Referenz für die Funktion mit mehreren Dateien

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 wie folgt dargestellt:

#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 wie folgt dargestellt:

#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; 
} 

Ich weiß, diese Frage vor gefragt wurde, aber ich konnte keine Antwort finden, dass ich richtig verstehen. Vielen Dank.

+3

Wie Sie Ihr Programm bauen? Sie verknüpfen mit allen Objektdateien oder verwenden alle Quelldateien beim Erstellen? –

+0

Was ist die 'sum.cpp' Datei? – Daniel

+0

http://idownvotedbecau.se/beingresponsive –

Antwort

0

Sie sollten sumptwonums.cpp in zwei Dateien teilen, damit sie funktionieren.

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; 
} 
Verwandte Themen