-1

I have the following 3 files, main.cpp and then a class defintion including a header file for the class:

main.cpp:

#include <iostream>
#include "data_vars_class.hpp"

int main () {
    
    DataVars dataVars();
    return 0; 
    
}
    

data_vars_class.hpp:

#include <string>
#include <vector>

class DataVars
{
    private:

        std::vector<std::string> csv_card_names;

    public:
    
        DataVars();
        void getCSVData();
}; 

data_vars_class.cpp:

#include <iostream>
#include <vector>
#include <string>
#include "data_vars_class.hpp"

DataVars::DataVars()
{
    std::cout << "constructor?";
    
    getCSVData();
}

void DataVars::getCSVData() 
{
    std::cout << "Getting csv data!";
}

The problem is when I build and execute the code, I just get an empty terminal. I know that both data_vars_class.hpp and data_vars_class.cpp are being included with the build, this is my build command in Geany:

g++ main.cpp data_vars_class.cpp -o a.out

How come i'm not seeing the cout output in the terminal, like in the constructor shouldnt i see "constructor?" in the terminal?

Thanks

Geoff L
  • 765
  • 5
  • 22
  • Try also `cout`ing an `std::endl` after text, to ensure you are flushing the buffers to console. – underscore_d Jun 29 '20 at 11:05
  • 3
    Are you running the program after building it? – Caleth Jun 29 '20 at 11:07
  • I added << endl to the cout and it made no difference. Yes I'm executing the program a.out, hence the empty terminal – Geoff L Jun 29 '20 at 11:08
  • 3
    `DataVars dataVars();` declares a function named `dataVars` that takes no arguments and returns an object of type `DataVars`. To create an object named `dataVars`, remove the `()`. – Pete Becker Jun 29 '20 at 11:10
  • @PeteBecker ah, yes, so dupe of [My attempt at value initialization is interpreted as a function declaration, and why doesn't A a(()); solve it?](https://stackoverflow.com/questions/1424510/my-attempt-at-value-initialization-is-interpreted-as-a-function-declaration-and) – underscore_d Jun 29 '20 at 11:11
  • ah thank u stupid mistake – Geoff L Jun 29 '20 at 11:13
  • 1
    @underscore_d -- sort of, but that one gets lost in the weeds when it talks about the most vexing parse. That's a complication that isn't needed to understand that this is **just** a function declaration, just like `int f();`. – Pete Becker Jun 29 '20 at 11:14
  • @GeoffL The other, IMO better option is to replace the `()` with uniform initialisation: `{}`, instead of just removing the initialiser; this is because I like to be in the habit of explicitly value-initialising everything, so that I won't accidentally have an uninitialised basic type later. – underscore_d Jun 29 '20 at 11:18

1 Answers1

0

by building the binary file gets created to run the code you have to write ./a.out in the terminal after building it.

g++ main.cpp data_vars_class.cpp -o a.out
./a.out
just a guy
  • 137
  • 7