error: macro names must be identifiers [closed] - c++

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 5 years ago.
Improve this question
I keep getting the error "macro names must be identifiers" in the following code, and I'm not sure why. I haven't violated any of the naming standards as far as I know. This is from my "dllist.h" file:
#ifndef _DOUBLY_LINKED_LIST_
#define _DOUBLY_LINKED_LIST_
template <class T>
class IntDLLNode {
friend class IntDLList;
 
public:
IntDLLNode() {next = prev = NULL;}
IntDLLNode(const T& el, IntDLLNode *n = NULL, IntDLLNode *p = NULL) {
info = el;
next = n;
prev = p;
   }
protected:
T info;
IntDLLNode<T> *next, *prev;
};
template <class T>
class IntDLList {
public:
IntDLList() {head = tail = NULL;}
void addToDLLTail(const T& el);
void addToDLLHead(const T& el);
T deleteFromDLLTail();
T deleteFromDLLHead();
void deleteNode(int);
void isInList(int) const;
void addSorted(int);
void printList();
private:
IntDLLNode<T> *head, *tail;
};
#endif
I've also tried names like DOUBLY_LINKED_LIST and DOUBLYLINKEDLIST -- all result in the same error.

You are violating a rule. _DOUBLY_LINKED_LIST_ is an invalid identifier. Anything that starts with an underscore, immediately followed by an upper-case character is reserved for the implementation.
Try DOUBLY_LINKED_LIST.
Also try to #include <cstddef> for NULL. Or use 0. Or better yet, if you have C++11 support, use std::nullptr.

try
#if !defined( _DOUBLY_LINKED_LIST_)
instead of
#ifdef _DOUBLY_LINKED_LIST_

Related

C++ event system with templates - how to store subscriber functions taking arbitrary type? [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 3 years ago.
Improve this question
I'm trying to write an event system using templates for flexibility and std::strings as event IDs. I want to be able to fire an event with an argument of any type and then a list of functions handles the event. My problem is I'm not very familiar with templates, so I can't figure out how to store a list of subscriber functions which take an arbitrary type.
I'm basically trying to do this, it's completely invalid pseudocode, but you should get the general idea of the resulting system I want:
template<typename T> std::map<std::string, std::vector<std::function<void(T)>>> subscribers;
template<typename T> void subscribe(std::string event_id, std::function<void(T)> f)
{
subscribers<T>[event_id].push_back(f);
}
template<typename T> void fire(std::string event_id, T data)
{
for (auto f : subscribers<T>[event_id])
f(data);
}
...
void handle_test_event(int data)
{
printf("%d", data);
}
subscribe<int>("test event", handle_test_event);
fire<int>("test event", 123);
What are the proper methods and syntax to achieve something functionally equivalent to the pseudocode?

out-of-line definition of 'tedt' [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 4 years ago.
Improve this question
Sorry if my English won't be clear for you, I'm a Russian student.
I have a code of a class with function declarations:
Account.h
class Account {
public:
Account();
virtual ~Account() = 0;
void tedt(const std::string &);
Account(std::string);
}
Account.cpp:
#include "Account.h"
void Account::tedt(const std::string& a) <===== error here
{
return;
}
Account::Account()
{
Account(""); <==== some other error is here...
}
Account::Account(std::string input) <===== and here!!!
{
SetLogin(input);
SetProxy("");
}
I see this message:
error: out-of-line definition of 'tedt' does not match any declaration in 'Account'
end this
error: out-of-line definition of 'Account' does not match any declaration in 'Account' (about Account::Account(std::string input))
And I don't know to do. I'm using qt creator for coding if it is important
As pointed out, you need to put a semicolon after the closing bracket of your class definition.
Please also note that your constructor without arguments will not work as expected here. Instead, you create a new temporary object with the argument. This will not change your current object. Better use:
Account::Account() : Account("") {}
, which works as expected.

return std::map with a getter method [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 4 years ago.
Improve this question
As the title indicates I'm trying to grab on to a std::map container and return it.
I get the following error: invalid use of template-name 'std::map' without an argument list
Now I pretty sure the reason has to do with templates, but simply haven't found an example that describes my specific situation.
My program i rather simple since I'm a newbie on parantal leave. It consists of these files:
main.cpp
Movie_Archive.hpp
Movie_Archive.cpp
Movie.hpp
Movie.cpp
Helper.hpp
Helper.cpp
I don't think anyone wants me to paste all the code so I've pasted the parts that I belive to be vital to my question. Code below:
Movie_Archive.hpp
class MovieArchive {
private:
std::map <std::string, Movie> movie_archive;
public:
std::map getMovieArchive();
};
Movie_Archive.cpp
std::map MovieArchive::getMovieArchive() {
return movie_archive;
}
main.cpp
TheMovieArchive.controlArchiveStatus(TheMovieArchive.getMovieArchive(), TheMovieArchive.getTitle());
// Checks if the movie title already has been entered
Thank soo much for taking a look. I hope someone can find a solution.
Kind regards//Alle
You probably mean
class MovieArchive {
private:
std::map <std::string, Movie> movie_archive;
public:
std::map<std::string, Movie> getMovieArchive();
};
i.e., you have to provide the template parameters in the return type of the getter as well.
BTW: you probably want to write
class MovieArchive {
private:
std::map <std::string, Movie> movie_archive;
public:
const std::map<std::string, Movie> & getMovieArchive();
};
i.e. returning a const reference instead of a copy of the internal data structure.

Multiple headers, how to use function on Object [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 5 years ago.
Improve this question
How do I access a variable or typedef in a header from a method? It seems that the typedef isn't global even though I included the header file, why?
I have the following situation:
Snake.h
#ifndef SNAKE_H
#define SNAKE_H
#include <utility>
class Snake {
public:
Snake(int difficulty, int posX, int posY) : difficulty(difficulty) {
position.first = posX;
position.second = posY;
}
inline std::pair<int,int> const getPosition() {
return position;
}
private:
typedef std::pair<int, int> Point;
Point position;
};
#endif // !Snake.h
Movement.cpp
#include "Movement.h"
#include "Snake.h"
Snake moveDown() {
Point dummy = SnakeObject.getPosition();
return .....;
}
Now obviously this doesn't compile since there is stuff missing, but the compiler fails to recognize the Point type in the Movement.cpp file.
Also, do I need a Snake pointer in the Movement.h so I can use the snake object to call getPosition?
I'm sorry for the vague description, also your help is much appreciated.
Point is declared as a private class member, as such it is not accessible to non-class members.
Either make it a public class member, use the underlying std::pair type instead (like the declared return type of the method actually specifies), or assign the return value to an auto.

Making a private function public in C++ [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 8 years ago.
Improve this question
I am learning my first programming language C++ and I have issues with making a private function public. Can you guys help me to find the problem?
#include <iostream>
#include <string>
using namespace std;
class JadClass
{
public:
void setName(string x)
{
name = x;
}
string getName()
{
return name;
}
private:
string name;
};
int main()
{
JadClass jc;
jc.setName = "Jad Charara w\n";
cout << jc.getName();
system("pause");
return 0;
}
instead of
jc.setName = "Jad Charara w\n";
write
jc.setName("Jad Charara w\n");
First of all you have defined 2 functions in class JadClass with public access specifier,so please confirm access specifier of which function you want to change from private to public.
Second thing in main you are trying to call setName function.
jc.setName = "Jad Charara w\n";
The above function call should be in jc.setName("Jad Charara w\n"); format.