I divided my program into three files. All of them are in the same folder and run in the g ++ compiler as g++ main.cpp pkv.cpp pkv.h
But it gives an error - most likely, I incorrectly filled in the pkv.cpp files pkv.h. If you write the function to_dec() just in main.cpp , then everything works
Do not look at the code much: I have it different, but this is a simplified model.
//main.cpp // // #include <iostream> #include "pkv.h" using namespace std; int main(int argc, char* argv[]) { string example = "1 5 20 15 3 -6 -19" double res_dec = to_dec(example); //TO_DEC() - требуемая функция cout << "RESULT in DECIMAL: " << setprecision(30) << res_dec << endl; return 0; } Second file
// pkv.cpp // // #include <iomanip> #include <cmath> #include <string> double to_dec(string pkv) { double decimal; short sign = atoi(pkv.substr(0, 1).c_str()); for (int i = 0; i < 2; i++) { pkv.erase(0, pkv.find(' ') + 1); } while (pkv.find(' ') != -1) { decimal += pow(2, atoi(pkv.substr(0, pkv.find(' ')).c_str())); pkv.erase(0, pkv.find(' ') + 1); } decimal += pow(2, atoi(pkv.c_str())); return pow(-1, sign) * decimal; } And third
// pkv.h // // #ifndef PKV_H #define PKV_H double to_dec(string pkv); #endif
g++ main.cpp pkv.cppshould be enough - well, in terms of - the header files need not be compiled separately ... - Harryg++ main.cpp pkv.cpppkv.h,std::stringused inmain.cppandpkv.h, but there is no#include <string>. - VTT