0

In a program the following struct is defined in a header file:

\\structs.h
#include <vector>
using std::vector;
using namespace std;
struct cell
{
    double x;
    vector<int> nn;
};

In a separate source file I define the function:

\\functions.cpp
# define _CRT_SECURE_NO_DEPRECATE
# include <stdio.h>
# include <iostream>
# include <math.h>
# include <vector>
# include "structs.h"
using namespace std;

void initial_position(vector<cell>& cluster, int n)
{
    cell tmp;
    for (int i = 0; i < n; i++)
    {
        tmp.x = 1;
        cluster.push_back(tmp);
    }
}

with a header file:

//functions.h
# include <vector>
using std::vector;

void initial_position(vector<cell>& cluster, int n);

I wish to call this function in the main script:

//main.cpp
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <vector>
#include "functions.h"
#include "structs.h"  
using namespace std;

int main()
{   
    vector <cell> cluster;
    int n = 100;
    initial_position(cluster,n);
    return 0;
}

but get the following errors:

functions.h(4): error C2065: 'cell': undeclared identifier

functions.h(4): error C2923: 'std::vector': 'cell' is not a valid template type argument for parameter '_Ty'

functions.h(4): error C3203: 'allocator': unspecialized class template can't be used as a template argument for template parameter '_Alloc', expected a real type

main.cpp(14): error C2664: 'void initial_position(std::vector &)': cannot convert argument 1 from 'std::vector>' to 'std::vector &'

What is the source of the errors? it all seems to be well defined.

jarhead
  • 1,821
  • 4
  • 26
  • 46
  • 2
    Add `# include "structs.h"` inside function.h (before the declaration of the problematic function). – Caninonos Jan 17 '18 at 07:36
  • 3
    `using namespace std;` in a header file? Are you working alone or just want to make people reject your patches? – StoryTeller - Unslander Monica Jan 17 '18 at 07:37
  • @StoryTeller, I don't understand the comment, can u pls explain? – jarhead Jan 17 '18 at 07:38
  • 2
    [It's already explained better, by others](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – StoryTeller - Unslander Monica Jan 17 '18 at 07:38
  • Never put "using namespace std" in a header file! std namespace will be accessible every time that file is included, which is very dirty. Find a more detailed explanation in the [CppCoreGuidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#sf7-dont-write-using-namespace-at-global-scope-in-a-header-file) – Blasco Jan 17 '18 at 07:57
  • Learn how to use/write header in C++. –  Jan 17 '18 at 08:09

3 Answers3

4

Put

#include "structs.h" 

into functions.h and protect both structs.h and functions.h with include-guards, e.g.

#pragma once

if available.

Jive Dadson
  • 16,680
  • 9
  • 52
  • 65
2

add

#include "structs.h" 

into functions.h since in functions.h compiler doesn't know what cell is.

Shawn
  • 82
  • 4
-2

Just swap

#include "functions.h"
#include "structs.h"  

with

#include "structs.h"  
#include "functions.h"

Since cell is declared in structs.h and is needed in functions.h

Or better yet, include structs.h in functions.h

You should also not place using namespace std in a header, that is bad practice. It can cause some nasty hard to find bugs, see e.g. Why is "using namespace std" considered bad practice?

AndersK
  • 35,813
  • 6
  • 60
  • 86
  • 4
    `#include`s order should never affect program compilation – Michał Walenciak Jan 17 '18 at 07:41
  • 4
    If `functions.h` needs `structs.h`, then it should include that itself. Since C++17, `std::vector` has minimal support for incomplete types, too, so a forward declaration is sufficient as long as the full definition is included before use. – chris Jan 17 '18 at 07:41
  • 1
    I agree with you both – AndersK Jan 17 '18 at 09:35