0

I've been looking into Syntax Error C2061 for a while now, and I have come to understand that it is often caused by circular dependencies of header files. However, I believe I should've resolved this in my files yet I continue to have the issue.

Arc.h

#pragma once

#include <string>

using namespace std;

class Node;

class Arc
{
public:
    Arc(Node &p_destination, const string &p_mode);
    ~Arc();

private:
    string m_mode;
    Node* m_destination;
};

Node.h

#pragma once
#include <string>
#include <vector>

using namespace std;

class Arc;

class Node
{
public:
    Node(const string &p_name, const int &p_identifier, const float &p_latitude, const float &p_longitude);
    ~Node();

    void set_arcs(Arc* p_arc) { m_arcs.push_back(p_arc); } //Line that causes the error

private:
    std::vector<Arc*> m_arcs;
    //Other Private Variables removed

};

The header files have both been included in the corresponding cpp files. Any help on this matter will be greatly appreciated!

Edit: Full Error Message below

"Syntax Error: identifier 'Arc'"
Connor Blakey
  • 846
  • 1
  • 7
  • 8
Christopher Orchard
  • 1,267
  • 2
  • 12
  • 15
  • How can you have `Arc` have a member of type `Node` and then in `Node` have a member of type `Arc`? – EdChum Mar 21 '16 at 10:24
  • 1
    ([*Why is “using namespace std” in C++ considered bad practice?*](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice)) – Biffen Mar 21 '16 at 10:26
  • 2
    @LoadData Can't you put the implementation of `Node::set_arcs` in the `.cpp` file? – Biffen Mar 21 '16 at 10:28
  • 2
    @EdChum Although it *might* be a sign of bad design, I don't see how it's a problem here. One is a pointer type and the other is within a vector, so there's no infinite recursion. – Biffen Mar 21 '16 at 10:29
  • 3
    Please post the entire error message. – molbdnilo Mar 21 '16 at 10:30
  • @Biffen Indeed I can. I have always tried to keep any get/set behaviour in the header file, assuming its not too long. Even if I were to place the method implementation inside the .cpp file the C2061 error is still throw on the definition. – Christopher Orchard Mar 21 '16 at 10:31
  • @LoadData Now you're definitely going to have to show us the error message. As for where implementations go, I find putting *all of them* in `.cpp` files is the best rule. – Biffen Mar 21 '16 at 10:32
  • Have updated the original post with the full error message. @Biffen I suppose a part of that is personal preference. – Christopher Orchard Mar 21 '16 at 10:33
  • 2
    Has this code been copied and pasted (not retyped from memory), and is that the *first* error message? – molbdnilo Mar 21 '16 at 10:34
  • 1
    Unrelated: passing `int` and `float` by const reference is a pointless pessimisation. – molbdnilo Mar 21 '16 at 10:34
  • @molbdnilo Yup, copied it directly from my visual studio editor. It is also the first error message. – Christopher Orchard Mar 21 '16 at 10:35
  • @LoadData Tested in VS2012 - can't reproduce. Do the error originate from the header file? – Algirdas Preidžius Mar 21 '16 at 10:42
  • @AlgirdasPreidžius Yes, it is thrown on the 'set_arcs' implementation. I'm also running 2015 VS. – Christopher Orchard Mar 21 '16 at 10:45

2 Answers2

0

The problem is that the name "Arc" is already in use by a method in the global namespace. Either rename your class to an unused name or place it in a namespace which is not the global namespace.

Connor Blakey
  • 846
  • 1
  • 7
  • 8
-4

You have a circular dependecy in you files. Arc depends on Node and Node depends on Arx. This cannot work, because you must include Arc in Node and also Node in Arc. Forward declaration helps here a little bit but you put a using inside the header file. You shouldn't do that because then your Node and Arc is inside std. Look here for further clarification. "using namespace" in c++ headers

Community
  • 1
  • 1
Liachtei
  • 512
  • 1
  • 4
  • 11