I am trying to create a class which calls one of it's functions when created, but I am getting the following error when compiling:
g++ -std=c++11 -Wall -Wextra -Werror -pedantic-errors -DNDEBUG -c src/PuzzleSolution.cpp
src/PuzzleSolution.cpp:7:32: error: definition of implicitly-declared 'PuzzleSolution::PuzzleSolution()'
PuzzleSolution::PuzzleSolution()
^
src/PuzzleSolution.cpp:12:6: error: prototype for 'void PuzzleSolution::addRow()' does not match any in class 'PuzzleSolution'
void PuzzleSolution::addRow()
^
src/PuzzleSolution.h:19:10: error: candidate is: void PuzzleSolution::addRow(std::vector<unsigned int>&)
explicit PuzzleSolution();
^
src/PuzzleSolution.cpp:17:48: error: no 'void PuzzleSolution::addElement(unsigned int)' member function declared in class 'PuzzleSolution'
void PuzzleSolution::addElement(unsigned int id)
^
make: *** [PuzzleSolution.o] Error 1
Here is the header:
#include <vector>
using namespace std;
class PuzzleSolution {
private:
vector<vector<unsigned int>> sol;
public:
explicit PuzzleSolution();
void addRow();
};
Here is the cpp file:
#include "PuzzleSolution.h"
PuzzleSolution::PuzzleSolution()
{
addRow();
}
void PuzzleSolution::addRow()
{
this->sol.emplace_back();
}
What am I doing wrong?
The code as it is has no error. It compiles with GCC 4.8.2
Be sure that your header file is indeed what you have linked to. Most likely the header being included is different than the one you have actually posted here.
Side Note: Generally it is considered as a bad practice to put using namespace std; in a header file.
Found the issue:
There was a file in the src folder called PuzzleSolution.h.gch
#Quatin and #StoryTeller helped me to understand that this is a pre-compiled header, which the compiler kept using.
Once deleted, the project compiled and executed
Related
I am working on a project which uses the LLVM YAML I/O library. This is the documentation/tutorial that I am following:
https://www.llvm.org/docs/YamlIO.html
I am trying to replicate the example where you define a specialization on llvm::yaml::MappingTraits for a struct data type. This example is at the top of the page.
This is my code that I have written:
#include <cstdlib> /* for EXIT_FAILURE */
#include <string>
#include <vector>
#include "llvm/Support/YAMLTraits.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/YAMLParser.h"
using std::string;
using std::vector;
using llvm::outs;
using llvm::errs;
using llvm::yaml::ScalarEnumerationTraits;
using llvm::yaml::MappingTraits;
using llvm::yaml::IO;
using llvm::yaml::Input;
using llvm::yaml::Output;
struct Person {
string name;
int hatSize;
};
template <>
struct MappingTraits<Person> {
static void mapping(IO& io, Person& info) {
io.mapRequired("name", info.name);
io.mapOptional("hat-size", info.hatSize);
}
};
int main(int argc, const char **argv) {
Person tom;
tom.name = "Tom";
tom.hatSize = 8;
Person dan;
dan.name = "Dan";
dan.hatSize = 7;
std::vector<Person> persons;
persons.push_back(tom);
persons.push_back(dan);
Output yout(llvm::outs());
yout << persons;
return EXIT_SUCCESS;
}
It seems to me that I have replicated the example code that they have in that tutorial exactly. But when I try to compile the program (using makefile) I get this cryptic error message:
clang++ -I/usr/local/include -std=c++11 -fno-exceptions -fno-rtti -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -std=c++14 -fcxx-exceptions -g -Wall -c -o yaml_project.o yaml_project.cpp
In file included from yaml_project.cpp:12:
/usr/local/include/llvm/Support/YAMLTraits.h:1871:36: error: implicit instantiation of undefined template 'llvm::yaml::MissingTrait<std::vector<Person, std::allocator<Person> > >'
char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
^
yaml_project.cpp:153:10: note: in instantiation of function template specialization 'llvm::yaml::operator<<<std::vector<Person, std::allocator<Person> > >' requested here
yout << persons;
^
/usr/local/include/llvm/Support/YAMLTraits.h:307:8: note: template is declared here
struct MissingTrait;
^
1 error generated.
<builtin>: recipe for target 'yaml_project.o' failed
make: *** [yaml_project.o] Error 1
I don't think that the error is in the command that I am using to compile this program, because it has worked for me before to compile and link the LLVM libraries into my executable. I think that the problem is in the code, but I cannot identify what.
The code for the mentioned header file llvm/Support/YAMLTraits.h is here:
https://llvm.org/doxygen/YAMLTraits_8h_source.html
Reading the documentation, it seems to me that support for your specific vector<Person> requires registration with a macro:
LLVM_YAML_IS_SEQUENCE_VECTOR(Person)
// or
LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(Person)
See, Utility Macros: https://llvm.org/docs/YamlIO.html#id22
I have a simple program containing the following code:
namespace nam
{
struct S{};
void f(S *){}
}
void f(nam::S *){}
int main()
{
nam::f(nullptr);
nam::S s;
f(&s);
return 0;
}
I expect that this will compile fine because I am calling f the second time without specifying namespace nam. However, upon compiling the code, I get this error:
$ g++ main.cpp -std=c++11 -Wall -Wextra
main.cpp: In function ‘int main()’:
main.cpp:14:9: error: call of overloaded ‘f(nam::S*)’ is ambiguous
f(&s);
^
main.cpp:7:6: note: candidate: void f(nam::S*)
void f(nam::S *){}
^
main.cpp:4:10: note: candidate: void nam::f(nam::S*)
void f(S *){}
Compiler and version:
$ gcc --version
gcc (Debian 5.3.1-14) 5.3.1 20160409
After trying this with different compilers, similar errors are returned. This seems to be a defined part of C++. I can't find anywhere on the internet where it says that calling a function with a struct in namespace nam as a parameter effectively implies using namespace nam; and requires ::f to remove ambiguity. I have 2 questions about this:
Where is this defined in the C++ standard?
Is there a good reason for this behavior?
Personally I like to avoid using namespace x; and similar. I want the compiler to give me an error when I don't specify a namespace. This behavior stops the compiler from doing so, and this means my code is inconsistent in places, because I occasionally forget to specify the namespace when calling functions like f that are not declared globally anywhere.
Your implementation of f(nam::S*) is outside of the namespace of 'nam'
change:
void f(nam::S *){}
to:
void nam::f(nam::S *){}
(or just move the enclosing namespace bracket) and all should be fine.
if your call to f(&s) in the current namespace was intentional then you need to specify this by changing the function call to
::f(&s)
As was said in the comments, this is due to argument-dependent lookup. I guess now I'll have to figure out now if I want to always specify the namespace in my code where this would make it unnecessary, or never specify it.
EDIT: the solution to the problem is the following: http://www.jusuchyne.com/codingchyne/2011/03/codeblocks-failed-to-find-the-header-file/
It won't compile, I have the following errors:
foo.h no such file in directory;
foo has not been declared;
num was not declared in this scope
foo is not a class or a namespace
It is odd, to say the least, because I just used the code blocks "Create a new Class" and then added it to this project. This is the source code:
Header:
#ifndef FOO_H
#define FOO_H
class foo
{
private:
int num;
public:
foo();
void set_num(int set);
int get_num();
};
#endif // FOO_H
the cpp
#include "foo.h"
foo::foo()
{
num = 10;
}
void foo :: set_num(int set)
{
num = set;
}
int foo :: get_num()
{
return num;
}
Disregard the calss itself and what it does, the problem is that it doesn't compile even though I used the default code blocks class creation setting.
The errors:
C:\Users\SameTime\Desktop\CodeBLocks\ASDD\src\foo.cpp|1|error: foo.h: No such file or directory|
C:\Users\SameTime\Desktop\CodeBLocks\ASDD\src\foo.cpp|3|error: 'foo' has not been declared|
C:\Users\SameTime\Desktop\CodeBLocks\ASDD\src\foo.cpp|3|error: ISO C++ forbids declaration of 'foo' with no type|
C:\Users\SameTime\Desktop\CodeBLocks\ASDD\src\foo.cpp||In function 'int foo()':|
C:\Users\SameTime\Desktop\CodeBLocks\ASDD\src\foo.cpp|5|error: 'num' was not declared in this scope|
C:\Users\SameTime\Desktop\CodeBLocks\ASDD\src\foo.cpp|6|warning: no return statement in function returning non-void|
C:\Users\SameTime\Desktop\CodeBLocks\ASDD\src\foo.cpp|8|error: 'foo' is not a class or namespace|
C:\Users\SameTime\Desktop\CodeBLocks\ASDD\src\foo.cpp||In function 'void set_num(int)':|
C:\Users\SameTime\Desktop\CodeBLocks\ASDD\src\foo.cpp|10|error: 'num' was not declared in this scope|
C:\Users\SameTime\Desktop\CodeBLocks\ASDD\src\foo.cpp|13|error: 'foo' is not a class or namespace|
C:\Users\SameTime\Desktop\CodeBLocks\ASDD\src\foo.cpp||In function 'int get_num()':|
C:\Users\SameTime\Desktop\CodeBLocks\ASDD\src\foo.cpp|15|error: 'num' was not declared in this scope|
||=== Build finished: 8 errors, 1 warnings ===|
If the header is not in the same directory you must either specify the path in the include command, or you must add -I Path directive to your makefile or include settings.
Maybe this link also helps as codeblock seems to have problems.
http://www.jusuchyne.com/codingchyne/2011/03/codeblocks-failed-to-find-the-header-file/
This should be a comment, but I don't have 50 rep yet...
Can you navigate to the source directory in the command line and try to compile manually to ensure that the error isn't with the IDE?
If your IDE is using g++ (it probably is) then the command would be g++ foo.cpp
Open Windows Explorer
Navigate to the folder containing the files
Make sure the header is called "foo.h" (You know Explorer sometimes hides file extensions, right?)
If that doesn't do it, your compiler is broken.
I would like to use g++ and -Werror, so I have now to disable warnings for 3rd-party libraries I have no control of. The solution provided by http://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html works very well, allowing simply to wrap the includes of 3rd party headers with pragmas. Unfortunately, that did no longer work for me in a certain setup where templates are involved. I created the following minimal example of where this approach did not work as expected:
Source file main.cpp
#pragma GCC diagnostic ignored "-Wunused-parameter"
#include "hdr.hpp"
#pragma GCC diagnostic error "-Wunused-parameter"
int main() {
return mytemplatefunc(2) + mystandardfunc(3); // will print ONLY ONE warning
}
and the header hdr.hpp
template<typename T>
int mytemplatefunc(T t) {
return 42;
}
int mystandardfunc(int i) {
return 53;
}
compiled using Makefile
CPPFLAGS+=-Wunused-parameter -Werror
main: main.cpp
will produce the following compiler error
g++ -Wunused-parameter -Werror main.cpp -o main
In file included from main.cpp:3:
hdr.hpp: In instantiation of ‘int mytemplatefunc(T) [with T = int]’:
main.cpp:29: instantiated from here
hdr.hpp:2: error: unused parameter ‘t’
make: *** [main] Error 1
shell returned 2
Note that explicit instantiation in main.cpp directly after including the header did not work, and wrapping the call to the template function in main.cpp did not work either. What was puzzling that putting #pragma GCC diagnostic ignored "-Wunused-parameter" in front of the main function silenced the compiler, whilst then adding #pragma GCC diagnostic error "-Wunused-parameter" at the very end of the file caused the compiler to produce the error again. How to solve this puzzle?
(Note, there are dozens of threads about this pragma, but I could not find anyone
that involved such a setup)
The issue is that the instantiation of the template is compiled when you use it, not when it is parsed by the compiler in the header file so it will not issue the warning until it replaces T by int and parses it as a regular function outside the context of the pragma silencing.
The usual way to indicate that you don't intend to use a parameter is to not give it a name:
template<typename T>
int mytemplatefunc(T /* t */)
{ return 42; }
int mystandardfunc(int /* i */)
{ return 53; }
This error is inexplicably occurring. Here is the code and output:
timer.cpp:
#include "timer.h"
#include "SDL.h"
#include "SDL_timer.h"
void cTimer::recordCurrentTime()
{
this->previous_t = this->current_t;
this->current_t = SDL_GetTicks();
}
timer.h:
#include "SDL.h"
#include "SDL_timer.h"
class cTimer
{
private:
int previous_t;
int current_t;
float delta_time;
float accumulated_time;
int frame_counter;
public:
void recordCurrentTime();
float getDelta();
void incrementAccumulator();
void decrementAccumulator();
bool isAccumulatorReady();
void incrementFrameCounter();
void resetFrameCounter();
int getFPS();
};
Compiler errors:
make
g++ -Wall -I/usr/local/include/SDL -c timer.cpp
timer.cpp: In member function ‘void cTimer::recordCurrentTime()’:
timer.cpp:6: error: ‘class cTimer’ has no member named ‘previous_t’
timer.cpp:6: error: ‘class cTimer’ has no member named ‘current_t’
timer.cpp:7: error: ‘class cTimer’ has no member named ‘current_t’
make: *** [timer.o] Error 1
Compiler errors after removing the #include "timer.h"
g++ -Wall -I/usr/local/include/SDL -c ctimer.cpp
ctimer.cpp:4: error: ‘cTimer’ has not been declared
ctimer.cpp: In function ‘void recordCurrentTime()’:
ctimer.cpp:5: error: invalid use of ‘this’ in non-member function
ctimer.cpp:5: error: invalid use of ‘this’ in non-member function
ctimer.cpp:6: error: invalid use of ‘this’ in non-member function
make: *** [ctimer.o] Error 1
Works for me. Are you sure you've got the right timer.h? Try this:
cat timer.h
and verify that it's what you think it is. If so, try adding ^__^ at the beginning of your .h file and seeing if you get a syntax error. It should look something like this:
[/tmp]> g++ -Wall -I/tmp/foo -c timer.cpp
In file included from timer.cpp:1:
timer.h:1: error: expected unqualified-id before ‘^’ token
This seems very odd as
class cTimer
{
private:
int previous_t;
int current_t;
float delta_time;
float accumulated_time;
int frame_counter;
public:
void recordCurrentTime();
float getDelta();
void incrementAccumulator();
void decrementAccumulator();
bool isAccumulatorReady();
void incrementFrameCounter();
void resetFrameCounter();
int getFPS();
};
void cTimer::recordCurrentTime()
{
this->previous_t = this->current_t;
this->current_t = SDL_GetTicks();
}
Compiles OK for me.
This suggests that the compiler think cTimer is different from what you've put in your header. So maybe its getting a definition of cTimer from another source file? For this to be the case your "timer.h" would have to not be gettting included correctly. So maybe the wrong timer.h.
A way to check this would be to save the compiler preprocessor output and search that for cTimer.
Another option might be to put a syntax error in your timer.h and make sure the compile fails.
Anyway hope this helps
Some compilers have their own timer.h, this is a name conflict.
Or it is a something else of bizarre bug...
Try renaming timer.h and timer.cpp to something more descriptive like ClassTimer.h and ClassTimer.cpp, maybe the compiler is linking another file named 'timer' since it is a very generic name. Also try this in timer.cpp:
void cTimer::recordCurrentTime(void)
{
this->previous_t = this->current_t;
this->current_t = SDL_GetTicks();
}
Edit: code edited