0

I'm being tasked with defining a struct within my main() function, but using it in other files. I have the code working if I define my struct inside my header file, but I cannot figure out how to define struct inside main() and still use it outside its given scope.

Example of what I have now:

3 files: main.cpp, header.h, and function.cpp

main.cpp

#include <iostream>
#include "header.h"

using namespace std;

int main()
{
    vector<myStruct> myVec;
    myFunction(myVec);
    return 0;
}

header.h

#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED
#include <vector>

using namespace std;

struct myStruct{
    int typeInt;
    string typeString;
    double typeDouble;
    };
void myFunction(vector<myStruct>&);

#endif // HEADER_H_INCLUDED

function.cpp

#include <iostream>
#include <vector>
#include "header.h"

using namespace std;

void myFunction(vector<myStruct>& myVec){
    myVec.push_back(myStruct());
    myVec[0].typeInt=5;
    cout<<myVec[0].typeInt<<endl;
    }

Right now, this works for what I need it to do. Unfortunately, I'm told I cannot define struct myStruct inside header.h but instead must have it within main() in main.cpp.

I've tried changing my code to the following (function.cpp unchanged):

main.cpp v2

#include <iostream>
#include "header.h"

using namespace std;

int main()
{
    struct myStruct{
        int typeInt;
        string typeString;
        double typeDouble;
    };
    vector<myStruct> myVec;
    myFunction(myVec);
    return 0;
}

header.h v2

#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED
#include <vector>

using namespace std;

template <typename myStruct>
void myFunction(vector<myStruct>&);

#endif // HEADER_H_INCLUDED

Now I receive the error: error: 'myStruct' was not declared in this scope on line 7 of function.cpp.

How can I use myStruct in function.cpp, while still defining myStruct in main() of main.cpp?

Emilio Garcia
  • 554
  • 1
  • 5
  • 20
  • What you were told to do makes absolutely no sense, whatsoever. C++ does not work this way. P.S. Don't ever, ever, [use `using namespace std;`, ever again](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – Sam Varshavchik Mar 22 '17 at 01:58
  • Okay, thank you. I'll refrain from using `using namepace std` as well. I just used it because it was inserted by default in Code::Blocks. – Emilio Garcia Mar 22 '17 at 02:01
  • If the struct is only defined in main.cpp, then `myFunction` cannot have a vector of it as parameter – M.M Mar 22 '17 at 02:01
  • @EmilioGarcia [The whole implementation of a template must reside in a header file](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). – Algirdas Preidžius Mar 22 '17 at 02:02
  • @AlgirdasPreidžius in most cases it's recommended, but there are some workarounds to be able to separate a template.h with a template.cpp implementation. From your same source's answer: http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file "...It is not necessary to put the implementation in the header file, see the alternative solution at the end of this answer." – Santiago Varela Mar 22 '17 at 02:18
  • Please review 'define' vs 'declare'. Did you misinterpret what someone told you? – 2785528 Mar 22 '17 at 02:20
  • @SantiagoVarela Yes, but did you even read said example? In those cases you must have an explicit instantiation of the template. Meaning - limiting the types the template can be used with. It is not what OP wants. – Algirdas Preidžius Mar 22 '17 at 09:09

1 Answers1

0

this way using template is correct, that problem occur in function.cpp right?

you try to define the function "myFunction(..)" in header.h, and remove function.cpp, this program will work well.

if you must implement that fuction in function.cpp, you have to create a template class.

D.Sam
  • 11
  • 2