There is a class library (I cannot/should not change) defined in MIDI.h:
template<class SerialPort, class _Settings = DefaultSettings>
class MidiInterface
{
...
I want to use this class as argument.
Question question is a duplicate, however I still get a compilation error.
My own header class file looks like:
#include <MIDI.h>
class M
{
public:
void setup();
void run();
private:
void printMessage(const MidiInterface<SerialPort, _Settings>&
midiInterface);
};
I get the following errors:
In file included from sketch\M.cpp:1:0:
M.h:10: error: 'MidiInterface' does not name a type
void printMessage(const MidiInterface<SerialPort, _Settings>& midiInterface);
^
M.h:10: error: expected ',' or '...' before '<' token
void printMessage(const MidiInterface<SerialPort, _Settings>& midiInterface);
^
exit status 1
'MidiInterface' does not name a type
How can I change the code so it compiles?
It looks like MIDI.h in this code puts everything in a namespace. If that's what you're using, try MIDI_NAMESPACE::MidiInterface<SerialPort, _Settings>.
Related
In IHello.hpp file
#include "trees.hpp" // EDITED this line
namespace world
{
class IHello
{
public:
struct hi
{
char a;
};
void func(plants a); //// EDITED this line
};
}
In Hello.hpp file
#include "IHello.hpp"
class Hello : public world::IHello
{
private:
void new_world(hi *init); // function in which struct is used
hi init; // initialization of structure , in this file it does not give error
};
In trees.hpp // different file where I want to use the structure
#include "IHello.hpp"
enum plants
{
cactus = 0x01
}
class trees
{
public:
void new_func(hi *b); // using that structure here, shows error of hi structure has not been declared
// 2nd method - void new_func(world::IHello::hi *b) // error that world has not been declared and 2nd error - error: expected ',' or '...' before '*' token
};
So what do I need to do in order to get the structure which is initialized in class IHello to be visible in class which does not inherit IHello?
There is no error in class Hello because it inherits class world::IHello. The structure hi is part of that class, and because it's not a private declaration, it is visible to and usable by Hello too.
The error in class trees is because this class does not inherit world::IHello and there is no type named hi within the naming scope of where that member function is declared. That type only exists within the class world::IHello.
To fix the error, you must qualify the name so that the compiler knows what you want:
void new_func(world::IHello::hi *b);
I want to overwrite a type with a typedef. The rationale for doing this is that one of my class has lot of templates and i want to replace all calls to that class with the templatized class (so that within another class Achild means Achild<T>. However, i get an error.
template <typename T>
class Achild
{
public:
Achild<T>() = default;
};
class Foo
{
typedef Achild<int> Achild;
public:
Foo() = default;
};
int main()
{
auto foo = new Foo();
}
I get the following error:
new_test.cpp:12:22: error: declaration of ‘typedef class Achild<int> Foo::Achild’ [-fpermissive]
typedef Achild<int> Achild;
^~~~~~
new_test.cpp:2:10: error: changes meaning of ‘Achild’ from ‘class Achild<int>’ [-fpermissive]
class Achild
How can i get this to work? The example is only for sample and my reasons for doing this is also related to how i have to get this to work with an existing codebase.
You are aliasing the type with itself (Achild already a type since you declared it in your class). You should instead do:
using Child = Achild<int>;
Note that you also should use the keyword using instead of typedef since it is the equivalent (and more robust) in C++. See What is the difference between 'typedef' and 'using' in C++11?.
I am a beginner of c++ meta programming.
Now I test a code which is on a text book.
The following code is a copy of the code but it give me an error message
at line(1). At line(1), it seems the code tries to call Layer2::function with template keyword because it is following another template. Unfortunately, my compiler give me an error message, "error: expected ‘;’ before ‘::’ token.". Can anyone find a problem of the code?
Thank you very much.
template<typename T>
class Layer0 {
public:
template<int L1>
class Layer1 {
public:
template<int L2>
class Layer2 {
public:
virtual void function(void);
};
};
};
template<typename T, int L1>
class LayerTest {
public:
void pointer (typename Layer0<T>::template Layer1<L1>::template Layer2<L1>* ptr) {
// At the following line(1), I got an error message, error: expected ‘;’ before ‘::’ token.
ptr->template Layer2<L1>::function(); // (1) inhibit call of virtual function
}
};
I have a class called A which looks like this:
class A
{
protected: typedef bool(*fp)();
public:
virtual void back(fp succesor,fp valid,fp solutie,fp show); // this function is defined in the .cpp
};
and a class B derived from A which looks like this:
class B : public A
{
public:
bool succesor(); //defined in cpp
bool valid(); //defined in cpp
bool solutie(); //defined in cpp
bool show(); //defined in cpp
void generate()
{
back(succesor,valid,solutie,nullptr);
}
};
I haven't shown you the constructors and the instantiation of the class, because it's not important in this phase.
The idea is that when I put that back call in the generate function is gives me these 3 errors:
error C3867: 'B::succesor': function call missing argument list; use '&B::succesor' to create a pointer to member
the same but with the other function name
the same but with the other function name
Please, could you suggest me where is my mistake?
Thank you
I find one of the most time-consuming compiler errors for me is "cannot instantiate abstract class," since the problem is always that I didn't intend for the class to be abstract and the compiler doesn't list which functions are abstract. There's got to be a more intelligent way to solve these than reading the headers 10 times until I finally notice a missing "const" somewhere. How do you solve these?
cannot instantiate abstract class
Based on this error, my guess is that you are using Visual Studio (since that's what Visual C++ says when you try to instantiate an abstract class).
Look at the Visual Studio Output window (View => Output); the output should include a statement after the error stating:
stubby.cpp(10) : error C2259: 'bar' : cannot instantiate abstract class
due to following members:
'void foo::x(void) const' : is abstract
stubby.cpp(2) : see declaration of 'foo::x'
(That is the error given for bdonlan's example code)
In Visual Studio, the "Error List" window only displays the first line of an error message.
C++ tells you exactly which functions are abstract, and where they are declared:
class foo {
virtual void x() const = 0;
};
class bar : public foo {
virtual void x() { }
};
void test() {
new bar;
}
test.cpp: In function ‘void test()’:
test.cpp:10: error: cannot allocate an object of abstract type ‘bar’
test.cpp:5: note: because the following virtual functions are pure within ‘bar’:
test.cpp:2: note: virtual void foo::x() const
So perhaps try compiling your code with C++, or specify your compiler so others can give useful suggestions for your specific compiler.
C++Builder tells you which method is abstract:
class foo {
virtual void x() const = 0;
};
class bar : public foo {
virtual void x() { }
};
new bar;
[BCC32 Error] File55.cpp(20): E2352 Cannot create instance of abstract class 'bar'
[BCC32 Error] File55.cpp(20): E2353 Class 'bar' is abstract because of 'foo::x() const = 0'