-1

Hi I'm having a problem using a struct as a parameter.

My struct looks the following:

typedef struct temps {
    string name;
    float max;
} temps;

I can use it in my main without problems like:

temps t;
t.max = 1.0;

but using it in a function signatures like this:

void printTemps(const temps& t) {
    cout << t.name << endl << "MAX: " << t.max << endl;
}

It gives me the following compiler message:

error C2563: mismatch in formal parameter list

Here is a mwe:

#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <Lmcons.h>

using namespace std;

typedef struct temps {
    string name;
    float max;
} temps;

void printTemps(const temps& t) {
    cout << t.name << endl << "MAX: " << t.max << endl;
}

int main(int argc, char **argv)
{
    temps t;
    t.max = 1.0;
    printTemps(t);
}

Any ideas whats wrong on the struct?

flor1an
  • 960
  • 3
  • 15
  • 33

1 Answers1

-1

You're using the C typedef struct declaration.

typedef struct temps {
    string name;
    float max;
} temps;

//void printTemps(const temps& t) {
void printTemps(const struct temps& t) {  // should match the C idiom
    cout << t.name << endl << "MAX: " << t.max << endl;
}

But since you are programming in C++, you should use the C++ way of declaring structs.

struct temps {
    string name;
    float max;
};

void printTemps(const temps& t) {
    cout << t.name << '\n' << "MAX: " << t.max << '\n';  // using \n is more efficient
}                                                        // since std::endl 
                                                         // flushes the stream
Michaël Roy
  • 6,338
  • 1
  • 15
  • 19
  • It's true that OP should avoid the typedef, however the typedef makes no change to the program (and therefore this change wouldn't fix the problem, unless the compiler is bugged or OP has not posted the real code, which is probably the case). – M.M Jul 19 '17 at 00:28