0

Working with C++ (Codeblocks 17.12 compiler), everytime program sees 'cout' in program.cpp i kept getting this message. Ideally need to create 'GoodAuto' object with three inserted variables (fuel_amount, double fuel_consumption and double best_speed); ability to change them with _change variables; ability to delete that 'GoodAuto' object.

Thanks in advance.

main.cpp

#include <iostream>
#include "program.h"
using namespace std;

int main(){
        Auto GoodAuto(200, 5, 60);
}
program.cpp

#include "lvtocon.h"
#include <iostream>
#include "program.h"
using namespace std;

Auto::Auto(double fuel_amount, double fuel_consumption, double best_speed)
{
    cout << "Enter fuel amount: " <<endl;
    this->fuel_amount = (fuel_amount>=0)?fuel_amount: 10;
    cout << "Enter fuel consumption for 100 km: " <<endl;
    this->fuel_consumption = (fuel_consumption>0)?fuel_consumption: 1;
    cout << "Enter optimal car speed: " <<endl;
    this->best_speed = (best_speed>0)?best_speed: 120;
}

void Auto::Change(double fuel_amount_change, double fuel_consumption_change, double best_speed_change)
{
    if (fuel_amount+fuel_amount_change>0) this->fuel_amount += fuel_amount_change; else fuel_amount = 0;
    if (fuel_consumption+fuel_consumption_change>0) this->fuel_consumption += fuel_consumption_change; else fuel_consumption = 1;
    if(best_speed + best_speed_change>0) this->best_speed += best_speed_change; else best_speed = 120;
}

void Auto::Print(){
    cout << "Fuel amount = " << fuel_amount << " l."<< endl;
    cout << "Fuel consumption for 100 km = " << fuel_consumption <<  " l/stunda." <<endl;
    cout << "Auto optimal speed = " << best_speed <<" km/stunda."<<endl;
}
program.h   

class Auto                                                   
{
private:
    double fuel_amount;
    double fuel_consumption;
    double best_speed;
public:

    Auto(double fuel_amount, double fuel_consumption, double best_speed);
    ~Auto();
void Change(double fuel_amount_change, double fuel_consumption_change, double best_speed_change);
void Print();
};
M.M
  • 138,810
  • 21
  • 208
  • 365
dac0n
  • 29
  • 1
  • 7
  • https://stackoverflow.com/q/12573816/5910058 – Jesper Juhl Dec 11 '19 at 22:22
  • Thanks, i'll look over it. But it does not really answer my question since i can understand where is my mistake but have no clue how to fix it. – dac0n Dec 11 '19 at 22:24
  • This error - a failure to link a standard library function - suggests a problem with your build system that we cannot possibly see from here, sorry. You would need to give much more information about your environment, tools, versions of your tools, settings, actions/commands... pretend we are not sitting there right with you because, well, we're not. – Lightness Races in Orbit Dec 11 '19 at 22:49
  • So... i can probably compile it if i just reinstall Codeblocks with default settings? – dac0n Dec 11 '19 at 22:51
  • ***i can probably compile it if i just reinstall Codeblocks with default settings?*** I am doubtful that reinstalling your IDE this will help at all. – drescherjm Dec 11 '19 at 22:55
  • And what to do with that kind of error? Where to find a way to solve this? – dac0n Dec 11 '19 at 22:57
  • You have to understand the build process. And debug it. Could be caused by a bad setting you changed in your project. – drescherjm Dec 11 '19 at 22:59
  • I tried to recreate project with default settings, but that doesn't help. – dac0n Dec 11 '19 at 23:07
  • Create a simple "Hello World" application. – drescherjm Dec 11 '19 at 23:13
  • Well, actually i recreated the project one time again, one file by another, and it worked. Maybe there something worked incorrectly when i added multiple files to the existing project. Not i'm messing with other mistaked :D – dac0n Dec 11 '19 at 23:18
  • That is unusual, I don't have a good reason. – drescherjm Dec 11 '19 at 23:19
  • Well.. actually it only worked because i inserted #include "program.cpp" to the main.cpp, but it took me to the "redefinition of class" error. well, since i can't really solve it now, gotta go for sleep, maybe tomorrow i'll find the answer (or delete this question) – dac0n Dec 11 '19 at 23:31
  • That means `program.cpp` was not part of your project. – drescherjm Dec 11 '19 at 23:32
  • Was ***undefined reference to operator<<(std::ostream&, char const*)** the exact text of the error message? – drescherjm Dec 11 '19 at 23:33
  • Yes, it is. Actually i get same error even if i try to include "cout <<" into main.cpp. – dac0n Dec 12 '19 at 07:17

2 Answers2

0

In my case, the problem was that I did not include all the necessary libraries in the project. For latvian language support i used library lvtocon.h and lvtocon.cpp, but since my program has include and didn't see the file, it would not run succesfully.

After adding all the necessary files to the project it compiled succesfully.

dac0n
  • 29
  • 1
  • 7
  • 1
    sounds like this header defines macros that change ``? What happens if you include it after `` instead? – M.M Dec 12 '19 at 22:46
  • The place of ` #include "lvfriendly.h" ` stroke does not affect the program. It's just a library that allows to use latvian symbols in the program, so i don't think it changes iostream. P. S. Sorry, i have messed up "" and <> in the other comment. – dac0n Dec 14 '19 at 08:37
  • Well your answer makes no sense then, `operator<<(std::ostream&, char const*)` is part of the standard library and has nothing to do with any addon – M.M Dec 14 '19 at 09:37
  • Well, it was the only step that helped me with that mistake. Nothing else worked and each time i returned my program to the original condition. – dac0n Dec 22 '19 at 08:00
-1

You don't seem to implement your destructor. Please try to include the implementation of ~A() {}.

ozox
  • 472
  • 2
  • 11
  • I made a function Auto::~Auto(){ cout << "Object was killed!" << endl; } But it does not help. And actually even if i try to include "cout" in my main.cpp, i get the exaxt error message. – dac0n Dec 12 '19 at 07:16
  • It is likely your compiler setup is not finding the library files for stdc++. – ozox Dec 12 '19 at 18:45
  • Thanks, you were close, i actually haven't added one necessary library. I have simplified and translated code to english to make it look better, but since i deleted line #include from the description it was impossible to find the answer. Should i delete my question? – dac0n Dec 12 '19 at 22:19