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'"
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.
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
Related
I'm trying to use a function within a class, which has a multimap as a parameter but I always get an error - "Overloaded function not found in 'TdOpenGLGraphs'" and I don't understand why. I made sure it was all public so there are no permission issues, I've declared it in the class in the .h file and the .cpp file exactly the same (I even got intellisense to do it for me so there were no typos) and no luck. I've included the correct header too but i still get the error
I also get:
"missing type specifier = int assumed. Note C++ does not support default-int."
and "syntax error: missing ',' before '<'"
even if I just declare a multimap as part of the class.
In the Header file:
class TdOpenGLGraphs
{
public:
//TdOpenGLGraphs();
//~TdOpenGLGraphs();
//This is the problem function
void CreateGraph(int iCurrGraphID, const multimap<int, double>& mDataToDisp);
public:
//This also produces an error
//multimap<int, double>mCurrData;
};
The .cpp File
#include "pch.h"
#include "framework.h"
#include "TdOpenGLGraphs.h"
using namespace std;
void TdOpenGLGraphs::CreateGraph(int iCurrGraphID, const multimap<int, double>& mDataToDisp)
{
}
Have you included the map header in the .h file? Also, avoid the
using namespace std; part, rather write everywhere std::map etc.
Im currently trying to implement a simple path-finding algorithm and need edges and nodes for it. I want to handle the implementation of those in one .h and one .cpp file. Right now I get the error "expected constructor, destructor or type conversion before ...".
I already tried separating both classes into 2 .h and .cpp-files, but that didnt work either. I've tried a lot of solutions provided for that error message, but nothing seems to work and I think there something Im missing right now.
My utilites.cpp file looks a bit like that
#include "utilities.h"
//Class Node
//Public
using namespace std;
Node::Node(string name)
{
this->name = name;
}
//Class Edge
//public
Edge::Edge(Node::Node nSource, Node::Node nTarget, int weight)
{
this->nSource = nSource;
this->nTarget = nTarget;
this->weight = weight;
}
and my utilities.h:
#ifndef UTILITIES_H
#define UTILITIES_H
#include <string>
#include <list>
class Node
{
public:
Node(std::string);
std::string name;
};
class Edge
{
public:
Edge(Node, Node, int);
Node nSource;
Node nTarget;
int weight;
};
#endif /* end of include guard: UTILITIES_H */
If I just use the Class Node, everything works.
But if I want implement Class Edge with the Class Node, I'll get the error previously mentioned. I think it is an easy solve, but I just cant figure it out.
I should say that I already tried it with
Edge::Edge(Node nSource, Node nTarget, int weight)
{
this->nSource = nSource;
this->nTarget = nTarget;
this->weight = weight;
}
but that just gave me the error "No matching function for call to 'Node::Node()'
The problem was that I was missing the curly braces after the default constructor of Node
Node(){};
Now it works as intended.
Thanks for the answers, they made me look at the default constructor closer again...
I am trying to compile a program that has 2 .h and 3.cpp . I keep getting the same error message: "error: ‘Patrons’ does not name a type Patrons match;"
when I put the whole code in one single .cpp file, I have no errors and it compiles, but for my assignment I need to do it in separate files.
I think I wrote the code right so I dont know why I get the error message.
// class patrons.h
using namespace std;
class Patrons //named it patrons because this is where i have a list of all the patrons
{
int patronscnt;
public:
std::list<string>::iterator PL;
std::list<string> patslist;
string name;
void patronslist();
void addpatron();
void removepatron();
void editpatron();
};
-
// class patron.h
using namespace std;
class Patron //class decleration. Named it patron because it has the information of one patron
{
string x;
string input;//class members
Patrons match;
public:
void ID();
void email();
void phone();
void address();
void borrowstatus();
void finestatus();
void check(string);
//update
};
You could include patrons.h in patron.h.
You accomplish this by adding the following to the top of the patron.h file:
#include "patrons.h"
However in my opinion, it is generally better to store a pointer to an object instead of the entire object. If you were to switch the match variable in patron.h to be a Patrons pointer:
Patrons *match;
Then instead of including patrons.h, you could forward declare the Patrons class, by adding the following to the top of the patron.h file:
class Patrons;
Then if needed you could include patrons.h in your patron.cpp file. Forward declaring will help you from running into circular dependencies.
You need to include Patrons.h in Patrons: #include "Patrons.h"
I have three classes.
first class:
#ifndef C_LINKED_LIST_H
#define C_LINKED_LIST_H
class CLinkedList {
private:
//removed code for brevity
public:
// removed code for brevity
};
#endif
second class:
#ifndef C_SSF_FOLDER_CONTAINER_H
#define C_SSF_FOLDER_CONTAINER_H
#include "C_SSF_Folder.h"
#include "CLinkedList.h"
class C_SSF_Folder_Container {
private:
// removed code for brevity
public:
int Add_Folder(C_SSF_Folder *_pcl_SSF_Folder);
C_SSF_Folder *Get_Folder(int _i_Index);
C_SSF_Folder *Get_Folder(char *_pch_Name);
//^-----errors
};
#endif C_SSF_FOLDER_CONTAINER_H
my third class
#ifndef C_SSF_FOLDER_H
#define C_SSF_FOLDER_H
#include <windows.h>
#include <fstream>
#include "C_SSF_Folder_Container.h"
using namespace std;
class C_SSF_Folder {
public:
private:
C_SSF_Folder_Container cl_SSFFC_Folder_Container;
public:
};
#endif
my third class C_SSF_Folder.
I am including "C_SSF_Folder_Container.h"
and declaring a C_SSF_Folder_Container container.
Before declaring the variable it compiles fine. After I declare it
I get syntax errors in my C_SSF_Folder_Container
Severity Code Description Project File Line Suppression State
Error C2061 syntax error: identifier 'C_SSF_Folder' CSSFileSystem\projects\cssfilesystem\cssfilesystem\c_ssf_folder_container.h 16
Error C2061 syntax error: identifier 'C_SSF_Folder' CSSFileSystem \projects\cssfilesystem\cssfilesystem\c_ssf_folder_container.h 19
As I myself look into it I think there is a problem because my C_SSF_Folder is including C_SSF_Folder_Container.
and C_SSF_Folder_Container is including C_SSF_Folder
but the defines should take care of it? Other than that I have no clue what's the problem.
Everything is typed correctly.
You've got a circular #include -- C_SSF_Folder_Container.h #includes C_SSF_Folder.h and C_SSF_Folder.h #includes C_SSF_Folder_Container.h.
This would cause an infinite regress (and a compiler crash) except that you've got the #ifndef/#define guards at the top of your files (as you should); and because of them, instead what you get is that one of those two .h files can't see the other one, and that's why you get those errors.
The only way to fix the problem is to break the circle by deleting one of the two #includes that comprise it. I suggest deleting the #include "C_SSF_Folder.h" from C_SSF_Folder_Container.h and using a forward declaration (e.g. class C_SSF_Folder; instead.
C_SSF_Folder.h and C_SSD_Folder_Container.h are including each other(Circular Dependency).
When the compiler compiles C_SSF_Folder_Container object, it needs to create a C_SSF_Folder object as its field, however, the compiler needs to know the size of C_SSF_Folder object, so it reaches C_SSF_Folder object and tries to construct it. Here is the problem, when the compiler is constructing C_SSF_Folder object, the object has a C_SSF_Folder_Container object as its field, which is a typical chicken and egg question, both files depends on each other in order to compile.
So the correct way to do it is to use a forward declaration to break the circular dependency(including each other).
In your C_SSF_Folder.h, make a forward declaration of C_SSF_Folder_Container.
#include <windows.h>
#include <fstream>
using namespace std;
class C_SSF_Folder_Container;
class C_SSF_Folder {
public:
private:
C_SSF_Folder_Container cl_SSFFC_Folder_Container;
public:
};
#endif
Finally, include C_SSF_Folder_Container.h in your C_SSF_Folder.cpp.
You can also learn more in the following links:
Circular Dependency (Wiki):
https://en.wikipedia.org/wiki/Circular_dependency
Forward Declaration by Scott Langham
What are forward declarations in C++?
I'm trying to integrate c++ code with awesomium functionalities, but I get many errors.
It seems that VisualStudio doesn't like my definition/declaration of the WebCore element. I copied it from http://wiki.awesomium.com/tutorials/tutorial-1-hello-awesomium.html.
I have simplified the code until this, and I still get the errors.
SimpleClass.cpp:
#include <Awesomium/WebCore.h>
include "SimpleClass.h"
using namespace Awesomium;
CSimpleClass::CSimpleClass(){
WebCore *web_core = WebCore::Initialize(WebConfig());
}
CSimpleClass::~CSimpleClass(){
}
SimpleClass.h:
class CSimpleClass
{
public:
CSimpleClass(void);
~CSimpleClass(void);
WebCore *web_core;
};
Thanks!
Change your SimpleClass.h header to read:
#pragma once
#ifndef SIMPLECLASS_H
#define SIMPLECLASS_H
// forward declarations
namespace Awesomium{
class WebCore;
}
class CSimpleClass
{
public:
CSimpleClass(void);
~CSimpleClass(void);
Awesomium::WebCore *web_core;
};
#endif /* SIMPLECLASS_H */
That way you announce to your compiler that there exists a type WebCore in the namespace Awesonium, and then you can use it to declare the member pointer CSimpleClass::web_core.
Potential dependency issues aside, the problem is that your header does not know that you want to use the Awesomium namespace.
Either (preferred) be explicit in the header about your definition of *web_core by doing
class CSimpleClass
{
public:
CSimpleClass(void);
~CSimpleClass(void);
Awesomium::WebCore *web_core; //note the use of Awesomium::
};
or (if you really must) include your header after your using directive
#include <Awesomium/WebCore.h>
using namespace Awesomium;
#include "SimpleClass.h"