Postingan lainnya
Error undefined reference to C++
ini file main.cpp di folder Sources
#include "hello.h"
int main(){
cout << "Test procedure Greeting" << endl;
string name = "Ashleen";
string id = "1301208111";
greetings(name,id);
cout<<"please answer question 1 "<<endl<<endl;
return 0;
}
ini file hello.cpp di folder Sources
#include "hello.h"
void greetings(string x, string y){
if(y.length() != 10){
cout <<"Wrong input"<<endl;
}else{
if(y.substr(0,2)=="13"){
cout<<"hello "<<x<<"from School of Computing."<<endl;
cout<<"you are majoring in ";
//int z = static_cast<int>(y[3])-48;
const chart *p=y.substr(3,1).c_str();
int z = atoi(p);
switch (z){
case 1:
cout<<"Informatics";
break;
case 2:
cout<<"Computing Science";
break;
case 3:
cout<<"Information Technolofy";
break;
default:
cout<<"..., where again?";
}
cout<<endl;
cout<<"and you'r the ";
string n= y.sustr(6,4);
if(n[3]=='1'){
cout<<n<<"st";
} else if(n[3]=='2'){
cout<<n<<"nd";
} else{
cout<<n<<"th";
}
cout<<" student listed in this major"<<endl;
cout<<"in "<<"20"+y.substr(4,2)<<endl;
} else{
cout<<"hello, you're not from School of Computing, "
<<"are you?"<<endl;
}
}
}
ini file hello.h di folder Headers
#ifndef HELLO_H_INCLUDED
#define HELLO_H_INCLUDED
#include <iostream>
#include <cstdlib>
using namespace std;
void greetings(string x, string y);
#endif // HELLO_H_INCLUDED
dan errornya adalah line 7 di file main undefined reference to greetings error : Id returned 1 exit status
1 Jawaban:
<div>Saya mencoba seperti ini dan bisa dijalankan:<br><br>main.cpp</div><pre>#include <iostream> #include "file-lain.h"
using namespace std;
int main() { cout << "Dari dalam main" << endl; greeting();
return 0;
}</pre><div><br>file-lain.cpp</div><pre>#include <iostream> #include "file-lain.h"
using namespace std;
void greeting() { cout << "Dari file lain" << endl; }</pre><div><br>file-lain.h</div><pre>#ifndef FILE_LAIN_H #define FILE_LAIN_H
void greeting();
#endif</pre><div><br>Lalu file-file di atas dicompile lalu dijalankan</div><pre>$ g++ main.cpp file-lain.cpp -o prog
$ prog Dari dalam main Dari file lain</pre>