[C++] Incomplete type error

Started by
6 comments, last by rip-off 13 years, 5 months ago
So I wanted to write my own cool timer which supports the calling of member functions.

I used the boost::function before and it worked very well, but this time I get an "Incomplete type"-error when using it.

My code goes as following:
#include <boost\functional.hpp>#include <boost\function.hpp>#include <boost\bind.hpp>/** Timer which ....    ... */class MemberFunctionTimer{public:	MemberFunctionTimer(void);	virtual ~MemberFunctionTimer(void);	/** Gives a function to this class. It will be called when the timer executes */	void Init(boost::function<void> Callback, float TimeToRun) // <----- Errors here!	{		CallbackFunction=Callback;		this->TimeToRun=TimeToRun;	}//...


Someone sees what I forgot?
Advertisement
void isn't a valid function signature. Did you mean boost::function<void (void)>?
That did it!

I always had return values when I used boost::function before, so I may confused that.
Quote:boost\bind.hpp

Note that directories in filenames in C and C++ are seperated with a slash, not with a backslash. Backslashes in #include filenames yield undefined behaviour:

Quote:2.9 Header names
If either of the characters ’ or \, or either of the character sequences /* or // appears in a q-char-sequence or a h-char-sequence, or the character " appears in a h-char-sequence, the behavior is undefined18

18->Thus, sequences of characters that resemble escape sequences cause undefined behavior.


For the rest of your code, read this one.



edit: I realise they have updated it (above was from n3000, this is from n3090)
Quote:If The appearance of either of the characters ’ or \, or of either of the character sequences /* or // appears in a q-char-sequence or a an h-char-sequence is conditionally supported with implementation-defined semantics,
or as is the appearance of the character " appears in a in an h-char-sequence , the behavior is undefined.18


The footnote says that depending on your compiler, this might be an error, an escape sequence, or something completely different (e.g. a windows path name under MSVC). Still, I think implementation-defined is not much better than undefined for non-systems-programmers, so I'd highly discourage this and use the well-defined slash instead.
Quote:
Note that directories in filenames in C and C++ are seperated with a slash, not with a backslash. Backslashes in #include filenames yield undefined behaviour:


Never got problems with this ... But just to stay save I'll change the slashes when everywhere when I used them (Just a very few times).

Quote:
#include <iostream>
#include <fstream>

int main()
{
#if 1
std::ifstream file("../test.dat"); // RIGHT!
#else
std::ifstream file("..\test.dat"); // WRONG!
#endif

...
}


I used this when dealing with files. Is it okay or will it break at some time?
std::ifstream file("..\\test.dat");


Edit: I just read the bottom of your linked FAQ. According to this, I may want to run a "Replace all" over my code and replace all "\\" with a "/".
Quote:I used this when dealing with files. Is it okay or will it break at some time?
std::ifstream file("..\\test.dat");


I guess it is okay as long as you don't leave windows, even though it is not the proper way. Personally, if I can have cross-platformness for free, then I grab it. Read: I never went back to backslashes :D

Note though that, imho, it is also more error prone, a backslash might be forgotten, but the path still looks correct upon code review.
Quote:Original post by mind in a box
I used this when dealing with files. Is it okay or will it break at some time?
std::ifstream file("..\\test.dat");
It will work fine on Windows, and not anywhere else.

If you are just writing software for windows, then there isn't any real harm in it, but '/' works everywhere, and is much less confusing to deal with (how many backslashes are needed to sprintf a backslash to a string and later use it with printf?).

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Forward slashes are safer because it is harder to accidentally do the following:
std::ofstream stream("C:\new.txt"); // whoops, that is a newline character!

This topic is closed to new replies.

Advertisement