c++ Class Header - "error: expected unqualified-id before ')' token" [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I must be overlooking something simple but I have so much to do in so little time that I might just be overworked. Anyway, here's where I'm getting the error.
#include<iostream>
#include<algorithm>
#ifndef Numbers
#define Numbers
class Numbers{
public:
Numbers(){capacity=5; used=0; data = new unsigned long[capacity];}
It's simply a constructor for the class Numbers. The private area of the class is as follows.
private:
unsigned long *data;
std::size_t used;
std::size_t capacity;
};
Again, it's probably something simple that I just can't see and I'm sure others run into the problem as well.
EDIT: error is as follows
numbers.h:9:11: error: expected unqualified-id before ')' token
Numbers(){capacity=5; used=0; data = new unsigned long[capacity];}
^

Run through the preprocessor, this:
#ifndef Numbers
#define Numbers
class Numbers{
public:
Numbers(){capacity=5; used=0; data = new unsigned long[capacity];}
becomes this :
class {
public:
() {capacity=5; used=0; data = new unsigned long[capacity];}
Your class name is the same as your include-guard fencepost macro. Since Numbers will be replaced with.. nothing.. you get no class name, nor constructor name. Don't do that.
Try:
#ifndef MYAPP_NUMBERS_H
#define MYAPP_NUMBERS_H
#include <iostream>
#include <algorithm>
class Numbers
{
public:
Numbers()
{
capacity=5;
used=0;
data = new unsigned long[capacity];
}
...etc...

Related

C++ constructor - error expected ')' before '*' token [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I've got this error "expected ')' before '*' token" and i don't get why:
This is my EventController.h code
#ifndef EVENTCONTROLLER_H_
#define EVENTCONTROLLER_H_
#include <iostream>
#include "EventModel.h"
#include "UserModel.h"
using namespace std;
#include <vector>
#include <stdexcept>
#include "Observer.h"
class EventController{
public:
EventController(EventModel *eventModel, UserModel *userModel);
virtual ~EventController();
void EventDoneUndone(bool& eventcurrentstate);
void addPerson2Event(UserModel *userModel, EventModel *eventModel);
void Update();
private:
EventModel *eventModel;
UserModel *userModel;
};
#endif /* EVENTCONTROLLER_H_ */
And this is my EventController.cpp where i get the error
#include "EventController.h"
#include <iostream>
#include "EventModel.h"
#include "UserModel.h"
EventController(eventModel *eventModel, userModel *userModel){ **HERE I GET THE ERROR**
this->eventModel = eventModel;
this->userModel = userModel;
// eventModel->attach();
// userModel->attach();
}
EventController::~EventController() {
// TODO Auto-generated destructor stub
}
void eventDoneUndone(EventModel eventModel1){
eventModel1.toggleState();
}
void addPerson2Event(UserModel userModel1, EventModel eventModel1) {
eventModel1.setPerson2Event(userModel1);
}
void EventController::Update(){ //maniera Pull Observer myObs
cout << "C'รจ stato un Update su";
}
Hope you guys can help me, i've already try to figure it out looking for solution in others guys problems but i failed.
[1] https://imgur.com/a/oXR8y
Did you mean:
EventController::EventController(EventModel *eventModel, UserModel *userModel)
// ^^^^^^^^^^^^^^^^^ ^ ^
?
The signature in your .cpp file is not the same as the one in your header.
EventController(EventModel *eventModel, UserModel *userModel) //Header
EventController(eventModel *eventModel, userModel *userModel) //Source
You're using CamelCase in your Header for EventModel and UserModel in the header, and in your source you're just writing lowercase.
Also You forgot to add EventController:: to your constructor and other methods in your source file.
I think you want use the constructor. So code have to be like this :
EventController::EventController(eventModel *eventModel, userModel *userModel){
this->eventModel = eventModel;
this->userModel = userModel;
// eventModel->attach();
// userModel->attach();
}

C++ error: redefinition of class [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Note: I have searched thoroughly on SO and the solutions posted for other's with similar questions are not working for me here.
I am writing my own custom 'string-like' class in C++, and am encoutering the following errors when compiling:
./PyString.h:8:11: error: out-of-line declaration of 'PyString' does
not match any declaration in 'PyString' PyString::PyString (char*);
^
./PyString.h:9:11: error: definition of implicitly declared destructor PyString::~PyString (void);
pystring.cpp:4:7: error: redefinition of 'PyString' class PyString {
As for the first and second errors, moving around the destructor into the class definition itself in the cpp file did not work.
As for the third error, I can't seem to fix it - I'm not redefining the class!
Here is pystring.h:
#ifndef PYSTRING_INCLUDED
#define PYSTRING_INCLUDED
class PyString {
char* string;
};
PyString::PyString (char*);
PyString::~PyString (void);
#endif
Here is pystring.cpp:
#include "PyString.h"
#define NULL 0
class PyString {
char* string = NULL;
public:
PyString(char inString) {
string = new char[inString];
};
~PyString(void) {
delete string;
};
};
For reference, here is the compile output as a screenshot:
Any help is greatly appreciated.
You're defining your class PyString in your header AND in your cpp file, and also, a function definition doesn't need a ; at it's end.
And... your function prototypes needs to be in your class declaration in your header :
pystring.h
class PyString {
public: //ALWAYS indicate what is public/private/protected in your class
PyString (char* inString);
~PyString (); // Don't put void when there's no parameter
private: // All attributes are private
char* string;
};
pystring.cpp
#include "PyString.h"
PyString::PyString(char* inString) {
string = inString; // Avoid using new unless you're forced to
}
PyString::~PyString() {
}
Oh yes you are! pystring.h contains
class PyString {
char* string;
};
which is a class declaration. The declarations PyString::PyString (char*);
and PyString::~PyString (void); need to be inside that declaration.
But you have something similar in pystring.cpp, specifying additional functions, and defining some of them. That's what your compiler is telling you.
Normally, you fully define a class in a header (i.e. all members, and the declaration of the member functions), and implement the member functions of that class in a source file.
The moral of the story here: you can't really learn C++ by trial and error. Get a good book!

Why cant i create a variable? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have two class: Clique and Graph,
when i try to create "Clique c" in the Graph class, VS give me two errors:
error C2065: 'Clique' : undeclared identifier
error C2065: 'c' : undeclared identifier
I tried to solve it for a long time and i'm stuck, pls help me.
this is my code:
Clique: http://pastebin.com/jw3FQv95
Graph: http://pastebin.com/bwLakmY0
thanks.
You have a circular inclusion, Graph.hpp includes Clique.hpp and Clique.hpp includes Graph.hpp.
Since to allocate a Clique in Graph class you need to know its full definition, you are forced to move the method that requires it in a separate source file (Graph.cpp) so that you will have something like
/* Clique.hpp */
#include "Graph.hpp"
class Clique { ... }
/* Graph.hpp */
// possibly a forward declaration to Clique here
class Graph {
...
Graph(const std::string &file, const double &th, const bool &debug, const bool &convert, const int &max_clique);
};
/* Graph.cpp */
#include "Clique.hpp"
Graph::Graph(const std::string &file, const double &th, const bool &debug, const bool &convert, const int &max_clique)
{
...
}

Redefinition of a class in C++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm new to C++ and stuck in a problem with an Error, called
"Semantic Issue: Redefinition of 'B' cB.h".
I have two classes, A and B, where cA should handle an Object of cB by reference and one friend function of cA, fExample. This is what the code looks like:
.h file cA:
#include "cB.h"
class A{
int val1, val2;
public:
friend void fExample(int, cB &);
};
.h file cB:
class B{
int val1, val2;
public:
void set_val1(int);
};
.cpp file cB:
#include <iostream>
#include "cB.h"
using namespace std;
void B::set_val1(int tVal){
val1 = tVal;
}
For me, it seems there is no way of working with the cB-object by reference with a friend function of cA. I would know some workarounds, but that's not my intention, I want to learn how to handle this problem the right way.
So thanks in advance for helping!
This type of error often happen due to missing include guards. The Simplest way is:
#ifndef HEADER_NAME
#define HEADER_NAME
You may also use #pragma once

I can't make a simple functor to compare string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this functor class :
#include <string>
using namespace std;
class IsPlayerOfType
{
public:
IsPlayerOfType(const string& type) : type_(type) {}
bool operator()(const Player* player) const
{
return (player->getType() == type_);
}
private:
string type_;
};
The class "Player" represent a player that has several methods and attributes. Among them, there is the method getType() which returns a string.
At some point of my program I have a variable called players_ which is of type vector<Player*>
Finally I have the following code to count the number of players of a certain type in my vector :
int number = count_if(players_.begin(), players_.end(), IsPlayerOfType("Defensive"));
When compiling I get a lot of errors such as :
error C2011: 'IsPlayerOfType' : 'class' type redefinition
error C2440: '' : cannot convert from 'const char [10]' to 'IsPlayerOfType'
error C2780: 'iterator_traits<_Iter>::difference_type std::count_if(_InIt,_InIt,_Pr)' : expects 3 arguments - 2 provided
I don't understand very well how count_if works, I tried to write this code inspiring myself from this answer : https://stackoverflow.com/a/13525420
I don't see where I'm wrong and the compiler errors confuse me.
My psychic debugging skills tell me that you forgot the #define include guards in the header that defines IsPlayerOfType causing the header to be multiply included in some source file. Keep in mind that #include works by the preprocessor doing text substitution which means the preprocessor would need additional logic to even attempt to prevent multiple inclusion.
Also note that using at file scope in a header is quite dangerous and should be avoided.