Awesomium c++: syntax error : missing ';' before '*' - 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"

Related

Having linking classes issue? c++ I have no clue what is up

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++?

Expected class name before { error

I am trying to get to grips with inheritance in C++ before trying to implement something in a larger file. I realise this question has been asked before but I've scoured literally everything I could find on this - nothing pointed me towards a fix. So hopefully a kind SO member can help me.
I writing a library for Arduino just to be clear. Here is my code:
CtrlBrd.h
#ifndef CtrlBrd_h
#define CtrlBrd_h
#include "Arduino.h"
class CtrlBrdClass
{
public:
CtrlBrdClass();
};
extern CtrlBrdClass CtrlBrd;
#endif
CtrlBrd.cpp
#include "Arduino.h"
#include "CtrlBrd.h"
CtrlBrdClass::CtrlBrdClass() {
}
int CtrlBrdClass::test()
{
return 79;
}
CtrlBrdClass CtrlBrd;
CtrlBrdEx.h
#ifndef CtrlBrdEx_h
#define CtrlBrdEx_h
#include <CtrlBrd.h>
class CtrlBrdEx : public CtrlBrdClass { // <----- Getting the error here!!
public:
CtrlBrdEx();
int test2();
};
extern CtrlBrdEx CtrlBrd;
#endif
CtrlBrdEx.cpp
#include "CtrlBrdEx.h"
int CtrlBrdEx::test2() {
return CtrlBrd.test() +1;
}
CtrlBrdEx CtrlBrd;
Error:
error: expected class-name before '{' token
Replace
#include <CtrlBrd.h>
with
#include "CtrlBrd.h"
The exact sequence of locations searched by the compiler is implementation dependent in both cases (ยง16.2 [cpp.include]), but both gcc and VC (and every other compiler if I had to guess) will search the current directory for the quoted form, but not necessarily for the other.
It seems the only solution is to include both files at the top of your main .ino code file. The Arduino compiler doesn't seem to like including libraries from within libraries...

Multiple inheritance error 'invalid use of incomplete type'

I have a dialog window settings that I want to display all values from my breadData object, and to do so I want to have settings inherit breadData's protected members. I try to forward declare breadData, but I'm getting a couple errors in my code.
/home/--/breadPull/prj/settings.h:14: error: invalid use of incomplete type 'struct breadData'
/home/--/breadPull/prj/resultwnd.h:7: error: forward declaration of 'struct breadData'
For one, breadData is not a struct, why does the compiler think breadData is a struct? Secondly I don't understand what the second line is trying to say. My only guess is because there are a lot of circular dependencies in my program. Here's the relevant code:
settings.h
#include <QDialog>
#include "breaddata.h"
class breadData;
namespace Ui {
class Settings;
}
class Settings : public QDialog, public breadData
{
Q_OBJECT
//.....
breadData.h
#include <vector>
#include <string>
#include <QtWidgets>
#include <QMainWindow>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "resultwnd.h"
#include "settings.h"
class MainWindow;
class resultWnd;
class breadData
{
public:
breadData(std::string);
~breadData();
//read in data file that provides all information
bool readData();
//.......
resultWnd.h
include <QGroupBox>
#include "breaddata.h"
class breadData;
namespace Ui {
class resultWnd;
}
class resultWnd : public QGroupBox
//.....
You have a circular dependency. breaddata.h includes settings.h before declaring breaddata. settings.h requires the declaration of breaddata for the inheritance.
Thus the file the preprocessor creates when compiling a file that includes breaddata first looks like this (indentation to visualize the recursive insertion of included header files):
<content of breaddata.h>:
<content of vector, string, QtWidget, QMainWindow, mainwindow.h and ui_mainwindow.h>
...
<content of resultWnd>:
...
class breaddata; //forward declaration mentioned in the error message
...
<content of settings.h>:
...
class Settings : public QDialog, public breadData //DANG!
...
class breaddata { ... //too late
Summarized:
class breaddata; //forward declaration mentioned in the error message
...
class Settings : public QDialog, public breadData //DANG!
...
class breaddata { ... //too late
The solution here is to avoid the includes in breaddata.h, especially settings.h. If necessary, forward-declare Settings. The rule of thumb is to include in headers only if you must, and forward-declare whenever you can.
Your problem is that you have an incomplete understanding of the following:
The precompiler
The difference between declarations and definitions and purpose of using the former.
The purpose and use of namespaces
Without knowing more about your code than what you've shown, the following should solve your problem:
settings.h
#ifndef SETTINGS_H
#define SETTINGS_H
// Your code as above
#endif
breaddata.h
#ifndef BREADDATA_H
#define BREADDATA_H
// Your code as above
#endif
resultWnd.h
#ifndef RESULTWND_H
#define RESULTWND_H
// Your code as above
#endif
I suspect that this will not solve your problem entirely though. Based on your second error message, I suspect you have left important lines of code out of your question so nobody will be able to give you a definitive answer to solve your problem.
I suggest you edit the code in your question to include all lines that contain the breadData, Settings and resultWnd
It is possible to solve this, we just need to see how the three classes are bound together so we can help you untangle them.
The reason the compiler thinks you're using a struct is purely historical. The class keyword was introduced in C++ with the intention to replace the struct. Previously, in C, only the struct keyword exists. As far as I know, the only difference between structs and classes is the default access level. classes default to private while structs default to public. Otherwise, they have identical usage.

VC++ compiler doesnt see any of my namespacec or classes

I'm working on a project in c++ and I stuck with no idea what is wrong. I've writen 4 classes and everything looked fine during work (under visual studio 2010). VS 'saw' all the definition, i could use auto-fill and sudgestions, but when I tried to compile the project it sudennly went blind. It's like I didnt include headers or something (which I did). The strange thing is there is no problem with working with those classes on VS (i can ctrl+space for hint, list of attributes and methods and all that stuff) but when i try to compile it says "ClassName" is not a type.
Quick sample of problem below:
ProButton.cpp:
#include "ProButton.h"
using namespace pGUI;
ProButton::ProButton( ... )
: ProControl( ... )
{
...
}
ProButton.h:
#ifndef __PRO_BUTTON__
#define __PRO_BUTTON__
#include <string>
#include "ProControl.h"
namespace pGUI
{
class ProButton :
public pGUI::ProControl
{
public:
//attributes
...
public:
//methods
...
};
}
#endif
but compiler says:
Error 291 error C2653: 'ProButton' : is not a class or namespace name
for this line in ProButton.cpp: ProButton::ProButton( ... )
It also says:
Error 23 error C2039: 'ProControl' : is not a member of 'pGUI'
Error 24 error C2504: 'ProControl' : base class undefined
and all similar errors for whole project. I have no idea what is wrong. Looks like my VS broke :D
Of course those (...) means there is code there, just not that important for now. I can upload all solution somewhere fi it will help.
edit//
About namespaces, all header files (classes declaration) are defined in namespace with:
namespace pGUI{
class ProClass
{
};
}
all definitions for these classes (in ProClass.cpp) are using:
using namespace pGUI;
at the beginning.
I think the problem is with order of including files.
Im not sure how this is supposed to be done. So far i have 4 classes that:
class ProGUI:
has a pointer to ProContainer
includes: ProContainer and ProControl
class ProContainer:
has pointers to: ProGUI and ProControl
class ProControl:
has a pointer to ProContainer
includes ProButton
is a base class for ProButton
class ProButton:
is a sub-class of ProControl
Those classes also uses irrlicht library and I'm not sure where to include it.
I had it included in my main file just before #include "ProGUI.h". This is also the only include in main. ProGUI.h .
//EDIT 2 -> solved
It was a problem with includes. I needed to rethink my inclusion order and add some forward declarations. Anyway that all seemed strange and took me a lot of time to figure i out. Thx for help. :)
It seems that you are using following statement:
using namespace pGUI;
Just before the class declaration:
class ProControl
{
};
Instead of using following approach:
namespace pGUI
{
class ProControl
{
};
}
The using namespace, as it says uses a namespace. You need to explicitly put something a namespace using namespace keyword followed by braces!
using namespace pGUI informs the compiler that it should look in the pGUI namespace to resolve existing names.
To declare or implement something in a namespace you need to be more specific. with either:
namespace pGUI
{
ProButton::ProButton( ... ) : ProControl( ... )
{
...
}
}
or:
pGUI::ProButton::ProButton( ... ) : ProControl( ... )
{
....
}
Personally, I consider any use of using namespace to be a lazy programmer hack that completely defeats the point of namespaces. But I digress. :)

c++ headers keeping sane

The biggest problem I seem to run into when coding in c++ is the fact that you must declare a class before you can reference it. Say I have two header file like this...
Header1.h
#include "Header2.h"
#include <deque>
#include <string>
#include <iostream>
using namespace std;
class HelloPackage;
class Hello
{
public:
string Message;
HelloPackage * Package;
Hello():Message("")
{
}
Hello(string message, HelloPackage * pack)
{
Message = message;
Package = pack;
}
void Execute()
{
cout << Message << endl;
//HelloPackage->NothingReally doesn't exist.
//this is the issue essentially
Package->NothingReally(8);
}
};
Header2.h
#include "Header1.h"
#include <deque>
#include <string>
#include <iostream>
using namespace std;
class HelloPackage
{
public:
deque<Hello> Hellos;
HelloPackage()
{
Hellos = deque<Hello>();
}
int AddHello(string Message)
{
Hellos.push_back(Hello(Message,this));
}
void ExecuteAll()
{
for each(Hello h in Hellos)
h.Execute();
}
int NothingReally(int y)
{
int a = 0;
a += 1;
return a + y;
}
}
What I'm wondering is, is there any elegant solution for dealing with these issues? In say c#, and java, you're not restricted by this "linear" compiling.
Use header include guards, either "#ifndef / #define / #endif", or "#pragma once"
Put your code in a .cpp, not inline in the header
???
Profit
The reason this will work for you is because you can then use forward declarations of the class you want to reference without including the file if you so wish.
You are missing include guards
why define methods in the header?
Besides these problems with your code, to answer your question : normal way is to forward declare classes - not to include headers in headers (unless you have to).
If you follow a few basic rules, it is not awkward at all. But in comparison to e.g. Java or C#, you have to follow these rules by yourself, the compiler and/or language spec does not enforce it.
Other answers already noted that, but I will recap here so you have it in one place:
Use include guards. They make sure that your header (and thus your class definition) is only included once.
Normally, you will want to separate the declaration and implementation of your methods. This makes the header files more reusable and will reduce compilation time, because the header requires normally fewer #includes than the CPP (i.e. implementation) file.
In the header, use forward declarations instead of includes. This is possible only if you just use the name of the respective type, but don't need to know any "internals". The reason for this is that the forward declaration just tells the compiler that a certain name exists, but not what it contains.
This is a forward declaration of class Bar:
class Bar;
class Foo {
void foooh(Bar * b);
};
Here, the compiler will know that there is a Bar somewhere, but it does not know what members it has.
Use "using namespace xyz" only in CPP files, not in headers.
Allright, here comes your example code, modified to meet these rules. I only show the Hello class, the HelloPackage is to be separated into header and CPP file accordingly.
Hello.h (was Header1.h in your example)
#include <string>
class HelloPackage;
class Hello
{
public:
Hello();
Hello(std::string message, HelloPackage * pack);
void Execute();
private:
string Message;
HelloPackage * Package;
};
Hello.cpp
#include "Hello.h"
#include "HelloPackage.h"
using namespace std;
Hello::Hello() : Message("")
{}
Hello::Hello(string message, HelloPackage * pack)
{
Message = message;
Package = pack;
}
void Hello::Execute()
{
cout << Message << endl;
// Now accessing NothingReally works!
Package->NothingReally(8);
}
One question that may arise is why is the include for string is needed. Couldn't you just forward declare the string class, too?
The difference is that you use the string as embedded member, you don't use a pointer to string. This is ok, but it forces you to use #include, because the compiler must know how much space a string instance needs inside your Hello class.