I'm trying to run this program in another file(main.cc) but I get an error on this class, where this is the error.
ERROR
./Library.h:15:24: error: expected ')'
Library(Book[] &book, int ¤tNumOfBooks);
^
./Library.h:15:16: note: to match this '('
Library(Book[] &book, int ¤tNumOfBooks);
^
CODE SOURCE:
class Library{
public:
Book booksLibrary[MAX_ARR_SIZE];
int currentNumOfBooksLibrary;
Library();
Library(Book[] &book, int ¤tNumOfBooks);
void addBook(Book &book);
void print();
private:
}:
Related
This is the code snippet that's giving this error. The line has been marked in the code snippet.
Code
class ignition::gazebo::systems::amclPrivate
{
public: Model model{kNullEntity};
public: bool updateAmclPose(const EntityComponentManager &_ecm);
public: std::string fixed_frame_;
public: std::string robot_frame_;
public: double rate_;
};
void amcl::Configure(const Entity &_entity,
const std::shared_ptr<const sdf::Element> &_sdf,
EntityComponentManager &_ecm,
EventManager &_eventMgr)
amcl_nh_.setCallbackQueue(& amcl_queue_);
amcl_pub_ = amcl_nh_.advertise<geometry_msgs::PoseWithCovarianceStamped>("amcl",1,true);
thread_ptr_ = std::make_shared<std::thread>();
{
// Set process rate
ros::Rate r(this->rate_);
while (this->amcl_nh_.ok())
{
this->updateAmclPose(const EntityComponentManager &_ecm); ---This line gives error
this->amcl_pub_.publish(this->msg_);
this->amcl_queue_.callAvailable(ros::WallDuration(0.0));
r.sleep();
}
}
Error
error: expected primary-expression before ‘const’
this->updateAmclPose(const EntityComponentManager &_ecm);
Error(updated after changing the line)
error: no matching function for call to
‘ignition::gazebo::v3::systems::amcl::updateAmclPose(const
ignition::gazebo::v3::EntityComponentManager&)’
this->updateAmclPose((const EntityComponentManager &)_ecm);
PS: The code snippet given above is a minimal example. Full implementation can be found here
PS: To avoid confusion, I haven't pasted full code. Can anybody suggest a solution?
Thanks
This question already has answers here:
Resolve build errors due to circular dependency amongst classes
(12 answers)
Closed 2 years ago.
Have two files:
CIoHandler.h
#pragma once
#include "CSession.h"
class CIoHandler
{
public:
CIoHandler();
~CIoHandler();
void AuthenticateUser();
void ConvertUnicodeToAscii();
void DoNTLMAuth();
void GetAndSetSocket();
void GetElevatedToken();
void GetHeaderMessage();
void GetUserNameW();
void HandleOperatorMessage();
void Init(CSession *cSession);
void IsLocalAccount();
int IsLoopBack();
void IsSPNFQDNName();
int IsSPNLocalInterface();
void IsSPNPresentInRegistry();
void IsTimedOut();
void IssueReadFromSocket();
void OnDataFromSocket();
void OnReadFromPipeCompletion();
void ParseAndValidateAccount();
void ProcessAuthenticationLine();
void ProcessCommandLine();
void ProcessDataFromSocket();
void ReadRegistryKey();
void SendDetailsAndAskForLicense();
void SendMessageToClient();
void SendTerminateString();
void SetPrivilege();
void WriteMessageToClientDirectly();
void WriteToClient();
void WriteToServer();
void WriteToSocket();
};
CSession.h
#pragma once
#include "CIoHandler.h"
class CSession
{
private:
CIoHandler* _CIoHandler;
public:
CSession();
~CSession();
void CollectPeerInfo();
void FreeInitialVariables();
void GetRegistryValues();
void Init();
void IsAnAdministratorOrMember();
void SecpSetIPandPort();
void Shutdown();
void WaitForIO();
};
When compiling with Visual Studio 2019 get the following errors:
CSession.h(8,13): error C2143: syntax error: missing ';' before '*'
CSession.h(8,13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
CSession.h(8,26): error C2238: unexpected token(s) preceding ';'
CIoHandler.h(16,22): error C2061: syntax error: identifier 'CSession'
CIoHandler.h(16,22): error C2061: syntax error: identifier 'CSession'
Removing
#include "CIoHandler.h"
private:
CIoHandler* _CIoHandler;
From CSession.h it compiles
What do I need to so I can use CIoHandler in CSession class definition
You have a cyclic include dependency. To break the cycle replace
#include "CIoHandler.h"
with a forward declaration.
class CIoHandler;
I'm trying to use emscripten to compile a c++ class and expose bindings. I'm running into an error from the compiler.
#include <emscripten/bind.h>
#include <emscripten/emscripten.h>
using namespace emscripten;
class MyClass {
private:
int _year;
int _month;
int _day;
public:
MyClass() { }
MyClass(int year, int month, int day);
int getMonth();
void setMonth(int);
int getYear();
void setYear(int);
int getDay();
void setDay(int);
bool isLeapYear();
int daysInMonth();
void increment();
void decrement();
};
EMSCRIPTEN_BINDINGS(my_sample_class) {
class_<MyClass>("MyClass")
.constructor<>()
.constructor<int, int, int>()
.function("getMonth", &MyClass::getMonth)
.function("increment", &MyClass::increment)
.function("decrement", &MyClass::decrement)
.property("year",&MyClass::getYear, &MyClass::setYear )
//.property("month", &MyClass::getMonth, &MyClass::setMonth )
//.property("day",&MyClass::getDay, &MyClass::setDay )
;
}
The compiler has no problems with the constructors or the function binding. I run into a problem with the property binding. I only have one uncommented to minimize the errors that I get back (they are just repeats of the same error but for different lines). Here are the errors that I'm getting back.
In file included from MyDate.cpp:1:
In file included from ./MyDate.h:2:
emscripten/bind.h:1393:26: error: implicit instantiation of undefined template 'emscripten::internal::GetterPolicy<int (MyClass::*)()>'
auto gter = &GP::template get<ClassType>;
^
./MyDate.h:37:6: note: in instantiation of function template specialization 'emscripten::class_<MyClass, emscripten::internal::NoBaseClass>::property<int (MyClass::*)(), void (MyClass::*)(int)>' requested here
.property("year",&MyClass::getYear, &MyClass::setYear )
^
emscripten/bind.h:569:16: note: template is declared here
struct GetterPolicy;
^
emscripten/bind.h:1399:33: error: implicit instantiation of undefined template 'emscripten::internal::GetterPolicy<int (MyClass::*)()>'
TypeID<typename GP::ReturnType>::get(),
^
emscripten\1.38.21\system\include\emscripten/bind.h:569:16: note: template is declared here
struct GetterPolicy;
^
2 errors generated.
shared:ERROR: compiler frontend failed to generate LLVM bitcode, halting
I've looked up binding examples and it appears I'm using the right syntax. Does any one have any idea of what I might be doing wrong?
Found the problem!
The getter functions must be marked as const to avoid these errors.
EX:
int getMonth() const;
(Beginner programmer..) I'm following the style of a header file that worked fine, but I'm trying to figure out how I keep getting all of these errors when I compile. I am compiling with g++ in Cygwin.
Ingredient.h:8:13: error: expected unqualified-id before ‘)’ token
Ingredient.h:9:25: error: expected ‘)’ before ‘n’
Ingredient.h:19:15: error: declaration of ‘std::string <anonymous class>::name’
Ingredient.h:12:14: error: conflicts with previous declaration ‘std::string<anonymous class>::name()’
Ingredient.h:20:7: error: declaration of ‘int <anonymous class>::quantity’
Ingredient.h:13:6: error: conflicts with previous declaration ‘int<anonymous class>::quantity()’
Ingredient.h: In member function ‘std::string<anonymous class>::name()’:
Ingredient.h:12:30: error: conversion from ‘<unresolved overloaded function type>’ to non-scalar type ‘std::string’ requested
Ingredient.h: In member function ‘int<anonymous class>::quantity()’:
Ingredient.h:13:25: error: argument of type ‘int (<anonymous class>::)()’ does not match ‘int’
Ingredient.h: At global scope:
Ingredient.h:4:18: error: an anonymous struct cannot have function members
Ingredient.h:21:2: error: abstract declarator ‘<anonymous class>’ used as declaration
And here is my class header file...
#ifndef Ingredient
#define Ingredient
class Ingredient {
public:
// constructor
Ingredient() : name(""), quantity(0) {}
Ingredient(std::string n, int q) : name(n), quantity(q) {}
// accessors
std::string name() { return name; }
int quantity() {return quantity; }
// modifier
private:
// representation
std::string name;
int quantity;
};
#endif
I am confused by these errors and don't really know what I am doing wrong concerning the implementation of the class..
That's a funny one. You are essentially killing your class name by #define Ingredient - all occurrences of Ingredient will be erased. This is why include guards generally take the form of #define INGREDIENT_H.
You are also using name both for the member and the getter function (probably an attempt to translate C#?). This is not allowed in C++.
How about look on errors? variables and functions can't have same names. And include guard should never names such as class.
#ifndef INGREDIENT_H
#define INGREDIENT_H
class Ingredient {
public:
// constructor
Ingredient() : name(""), quantity(0) {}
Ingredient(std::string n, int q) : name(n), quantity(q) {}
// accessors
std::string get_name() const { return name; }
int get_quantity() const {return quantity; }
// modifier
private:
// representation
std::string name;
int quantity;
};
#endif
I started learning C++, classes, objects, structures and more, but I'm having some problems with this.
#include <iostream>
using namespace std;
class Owner
{
public:
// Getters
string GetName(){return info.name;}
int GetAge(){return info.age;}
short int GetGender(){return info.gender;}
// Setters
void SetName(string value){info.name = value;}
void SetAge(int value){info.age = value;}
void SetGender(short int value){info.gender = value;}
private:
struct info
{
string name;
int age;
short int gender;
};
};
class Pet
{
public:
// Getters
string GetName(){return info.name;}
int GetAge(){return info.age;}
short int GetGender(){return info.gender;}
// Setters
void SetName(string value){info.name = value;}
void SetAge(int value){info.age = value;}
void SetGender(short int value){info.gender = value;}
private:
struct info
{
string name;
int age;
short int gender;
}
};
int main()
{
// Creating object ...
cout << "qq" << endl;
return 0;
}
But I get these errors when I try to compile it:
In member function 'std::string Owner::GetName()':|
main.cpp|9|error: expected primary-expression before '.' token|
In member function 'int Owner::GetAge()':|
main.cpp|10|error: expected primary-expression before '.' token|
In member function 'short int Owner::GetGender()':|
main.cpp|11|error: expected primary-expression before '.' token|
In member function 'void Owner::SetName(std::string)':|
main.cpp|14|error: expected unqualified-id before '.' token|
In member function 'void Owner::SetAge(int)':|
main.cpp|15|error: expected unqualified-id before '.' token|
In member function 'void Owner::SetGender(short int)':|
main.cpp|16|error: expected unqualified-id before '.' token|
main.cpp|45|error: expected unqualified-id before '}' token|
In member function 'std::string Pet::GetName()':|
main.cpp|30|error: expected primary-expression before '.' token|
In member function 'int Pet::GetAge()':|
main.cpp|31|error: expected primary-expression before '.' token|
In member function 'short int Pet::GetGender()':|
main.cpp|32|error: expected primary-expression before '.' token|
In member function 'void Pet::SetName(std::string)':|
main.cpp|35|error: expected unqualified-id before '.' token|
In member function 'void Pet::SetAge(int)':|
main.cpp|36|error: expected unqualified-id before '.' token|
In member function 'void Pet::SetGender(short int)':|
main.cpp|37|error: expected unqualified-id before '.' token|
||=== Build finished: 13 errors, 0 warnings ===|
Why does it give me so many errors?
I don't know why, because it is obvious that, for example,
string GetName()
{
return info.name;
}
returns a string, from the structure info.name
I'm using CodeBlocks.
You're declaring the struct as a type (Owner.info) instead of as a member (this->info). You probably want this:
struct OwnerInfo
{
string name;
int age;
short int gender;
};
class Owner {
// stuff..
private:
OwnerInfo info;
};
Or, the more reasonable version would be just having them there directly instead of inside a pointless struct:
class Owner {
// stuff..
private:
string name;
int age;
short int gender;
};
You're misunderstanding the syntax of the struct keyword, furthermore the actual member variable has to be declared before the member functions accessing it. So change your class declarations to something like
class Owner
{
private:
struct
{
string name;
int age;
short int gender;
} info;
public:
// Getters
string GetName(){return info.name;}
int GetAge(){return info.age;}
short int GetGender(){return info.gender;}
// Setters
void SetName(string value){info.name = value;}
void SetAge(int value){info.age = value;}
void SetGender(short int value){info.gender = value;}
};
The declaration:
private:
struct info
{
string name;
int age;
short int gender;
};
... defines the layout of a nested structure type info, much like the definition of the entire class does. However, info is now a type nested within Owner, not an instance of that type that is a member of Owner. Instead, try naming the struct Info, then declaring Info info = new Info(); in the private section of Owner.
Well you created a struct and therefor told your compiler "info" is a typ with the following attributes...
You need to declare a variable of the type "info".
info personalInfo;
Declare it as class member and you can create your Get-er and Set-er.
string GetName(){return personalInfo.name;}
More Information
You should create a object of struct info. You just cannot access a field of a struct directly without creating an object.. Why do you need the struct at all? try this
class Owner
{
private:
string name;
int age;
short int gender;
public:
// Getters
string GetName(){return this->name;}
int GetAge(){return this->age;}
short int GetGender(){return this->gender;}
// Setters
void SetName(string value){this->name = value;}
void SetAge(int value){this->age = value;}
void SetGender(short int value){this->gender = value;}
};