Error : Type expected '=', ',', ';', 'asm' or '__attribute__' before '{' token

Hi,

I have created one Legato application project and in that created one class
while building the project, it gives error in ‘.h’ of class at declaration of class

error in class declaration on legato developer studio :
Type expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘{’ token

my ‘.h’ file code as below:

**#ifndef CFIRSTSAMPLECLASS_H_**
**#define CFIRSTSAMPLECLASS_H_**


**class CfirstSampleclass**
**{**
**public:**
**    CfirstSampleclass();**
**    virtual ~CfirstSampleclass();**

**    // additional variables and methods**

**};**

**#endif /* CFIRSTSAMPLECLASS_H_** */

Can any one tell me solution?

Hi @rups,

is your main source file named .c or .cpp?
Depending on the extension, GCC will consider your code to be in C or in C++ language!
My guess here is that your .h file is included by a .c file.

yes, my main file is ‘.c’ . I have created project with legato application option.
So, I can not generate C++ class in this project?
Is there other way build it ?

I have created a new project which I have created as below
new->project->c++ application->C++ Hello world Project->(toolchain)legato linux GCC->Finish

now I have build it but there symantic errror showing at ‘cout’, ‘endl’, ‘std’ : symbol could not be resolved.

code:

#include <iostream>
using namespace std;

int main() {
    cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
    return 0;
}

I just want to add code for my project in c++ language and burn it in mangOH borard for some application
how can create C++ project and build it successfully ?

You can do C++ in Legato apps.
Once your Legato app project is created, you can either rename the main .c file to .cpp, or add a new .cpp file.
The only thing to take care about is to explicitly declare pure C code fragments (and all usage of the Legato apis will be) as such in your .cpp files.

Example (if you rename the .c file to a .cpp file):

extern "C" {

#include "legato.h"

COMPONENT_INIT
{
    LE_INFO("Hello, world.");
}

}

// Some C++ stuff here
class name {
public:


private:
};

Inclusion of .h files containing C stuff will have to be done inside the extern “C” block, and inclusion of .h files containing C++ stuff will have to be done outside of it.

By the way, if you rename your .c file, please note that the Component.cdef file needs to be updated accordingly. Fortunately, this is managed automagically if you’re renaming the file inside DS.

Thank you so much now I am able to build it.

now I have added class in main ‘.cpp’ file and access that class method through one main() method.
this main() method I have called at COMPONENT_INIT micro , as in legato application this micro acts as initialization main method of application.
But I am facing one error related to class which I have generated in separate ‘cpp’ and ‘.h’ file.

code :
mainComponent.cpp

#include "CfirstSampleclass.h"

extern "C"{
#include "legato.h"

}


class mainClass
{

public:

    mainClass()
    {

    }

    ~ mainClass(){};

    void funcOne()
    {

     LE_INFO("Hello, Rupali.");

    }

};

int main()
{

mainClass obj;
obj.funcOne();

CfirstSampleclass firstObj;
firstObj.ffoo();

return 0;

}


extern "C" {

COMPONENT_INIT
{
    LE_INFO("Hello, world.");
    main();
}
} 

CfirstSampleclass.cpp code:

#include "CfirstSampleclass.h"
extern "C"{
#include "legato.h"
}

CfirstSampleclass::CfirstSampleclass() {

}

CfirstSampleclass::~CfirstSampleclass() {
    // TODO Auto-generated destructor stub
}

void CfirstSampleclass::ffoo()
{
    LE_INFO("Now in to CfirstSampleclass: ffoo() ");

}

I am getting error as :
undefined reference to CfirstSampleclass::CfirstSampleclass()' undefined reference to CfirstSampleclass::ffoo()’

Though I have included header of this cpp file, I am getting this reference error.
please help me to get out of it.

Mmmm, just looks like your CfirstSampleclass.cpp file is not referenced in your Component.cdef file.

Tip: use Ctrl+space for auto-completion in the sources section.

(By the way, in next DS release, we’re working on an improvement to automatically update the Component.cdef file when source files are created/deleted)

Thank you so much. :slight_smile:
I have added manually ‘.cpp’ name in ‘cdef’ file.
now able to build and run successfully.