2

I just switched from Python to C++. This is a practice I did for struct. There's always an error 'incomplete type is not allowed' if I don't directly use struct at the source file.

I have review many answers at stack overflow and tried to add typedef in header or remove struct at std::vector<double> timesteps(struct temporal_info time), but none of them work.

Here's my dmdbase.h

#ifndef dmdbase
#define dmdbase

#include <iostream>
#include <vector>

class DMDBase

{
public:
    struct temporal_info
    {
        double t0;
        int trend;
        double dt;
    };
    std::vector<double> timesteps(struct temporal_info time);
};
#endif

Here's my dmdbase.cpp

using namespace std;


std::vector<double> timesteps(struct temporal_info time)
{
    std::vector<double> time_map;
    double final = time.trend + time.dt;
    for (double t = time.t0; t < final; t += time.dt)
    {
        time_map.push_back(t);
    }
    return time_map;
}
kappasalt
  • 23
  • 4
  • The answers of benroberts999 and andnik should solve your problem. However, you should favor andnik's answer, since he put the `DMDBase::` in the correct place. Another hint: Don't get used to `using namespace std`. It might get you into trouble: https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – wychmaster Mar 01 '20 at 10:30
  • Since benroberts999 corrected his answer, forget what I said about which answer to favor. ;) – wychmaster Mar 01 '20 at 10:38
  • @wychmaster you can erase your comments that have became incorrect after edits instead of correcting them with more comments. ;) – Öö Tiib Mar 01 '20 at 10:53

3 Answers3

4

You don't need the struct keyword in the function call site, that is not part of the type

std::vector<double> timesteps(temporal_info time);

Also: Issue is struct 'temporal_info' only defined inside the DMDBase class. So you have to do this

std::vector<double> DMDBase::timesteps(temporal_info time);
benroberts999
  • 393
  • 1
  • 10
3

In dmdbase.cpp make sure you specify that timesteps is a method of DMDBase class. And remove struct keyword before temporal_info as it was already mentioned.

std::vector<double> DMDBase::timesteps(temporal_info time)
{
    ...
}

The emphasis is on DMDBase::timesteps

andnik
  • 2,405
  • 2
  • 22
  • 33
-2

try this

#include <iostream>
#include <vector>


typedef struct temporal_info temporal_info;

class DMDBase

{
public:
    struct temporal_info
    {
        double t0;
        int trend;
        double dt;
    };
    std::vector<double> timesteps(temporal_info time);
};
yaodav
  • 1,126
  • 12
  • 34