when i compile my traingl
e_test.cpp program, i get following errors when gtest.h header file is included in the file.
In file included from c:\mingw\lib\gcc\mingw32\6.3.0\include\gtest\gtest-message.h:55:0,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\gtest\gtest-assertion-result.h:46,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\gtest\gtest.h:59,
from traingle_test.cpp:2:
c:\mingw\lib\gcc\mingw32\6.3.0\include\gtest\internal\gtest-port.h:1238:8: error: 'mutex' in namespace 'std' does not name a type
std::mutex mu_;
^~~~~
c:\mingw\lib\gcc\mingw32\6.3.0\include\gtest\internal\gtest-port.h:1239:8: error: 'condition_variable' in namespace 'std' does not name a type
std::condition_variable cv_;
^~~~~~~~~~~~~~~~~~
c:\mingw\lib\gcc\mingw32\6.3.0\include\gtest\internal\gtest-port.h: In member function 'void testing::internal::Notification::Notify()':
c:\mingw\lib\gcc\mingw32\6.3.0\include\gtest\internal\gtest-port.h:1225:21: error: 'mutex' is not a member of 'std'
std::lock_guard<std::mutex> lock(mu_);
^~~
c:\mingw\lib\gcc\mingw32\6.3.0\include\gtest\internal\gtest-port.h:1225:21: error: 'mutex' is not a member of 'std'
c:\mingw\lib\gcc\mingw32\6.3.0\include\gtest\internal\gtest-port.h:1225:31: error: template argument 1 is invalid
std::lock_guard<std::mutex> lock(mu_);
error messages
traingle_test.cpp
#include "traingle.h"
#include <gtest/gtest.h>
namespace {
TEST (TraingleTest, InvalidSides){
EXPECT_EQ(-1, TypeOfTraingle(-10, 20, 30));
EXPECT_EQ(-1, TypeOfTraingle(10, -20, 30));
EXPECT_EQ(-1, TypeOfTraingle(3, 4, -8));
}
}
traingle.cpp
#include "traingle.h"
/* Return values
1 for equilateral, 2 for isosceles, 3 for scalan
0 for traingle can't be formed with given sides
-1 if any side value is invalid, say -ve
*/
int TypeOfTraingle(int a, int b, int c){
if(a<0 || b<0 || c<0)
return -1;
if(!(a+b > c && b + c > a && a + c > b))
return 0;
else if (a==b && b==c)
return 2;
else
return 3;
}
traingle.h
#ifndef __TRIANGLE_H
#define __TRIANGLE_H
int TypeOfTraingle(int, int, int);
#endif
I have 3 files here traingle.cpp (calculations are done here), traingle_test.cpp (test cases are written) and traingle.h (contains function declaration). the test cases file need to be linked with traingle.cpp and output is how many test cases passed and type of traingle (equilateral, isosceles and scalene triangle) based on test case values(x, y, z) will be displayed as shown in below image
Your MinGW GCC (version 6.3.0) is very old.
You will need a MinGW-w64 that was built with POSIX threads support.
A standalone version is available for download from https://winlibs.com/ (separate downloads for 32-bit and 64-bit Windows)
Related
I'm trying to build a game that uses SDL2 (which compiles fine, but I get seg fault when executing the binary file) and SFML (which I can't get to compile successfully).
These are the errors I get:
pi#raspberrypi:~/spaceinvaders $ scons --use_sfml
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
g++ -o audio/player.o -c -Wextra -I. -DUSE_BOOST_CHRONO -DBOOST_SP_DISABLE_THREADS -DTIXML_USE_STL -O3 -DUSE_SFML -DNDEBUG audio/player.cpp
audio/player.cpp: In member function 'sf::SoundBuffer oci::audio::{anonymous}::Loader::operator()(const string&)':
audio/player.cpp:52:16: error: 'class sf::SoundBuffer' has no member named 'LoadFromMemory'
audio/player.cpp: In constructor 'oci::audio::{anonymous}::ControllerImpl::ControllerImpl(const sf::SoundBuffer&, bool, bool)':
audio/player.cpp:61:24: error: no matching function for call to 'sf::Sound::Sound(const sf::SoundBuffer&, bool&)'
audio/player.cpp:61:24: note: candidates are:
/usr/local/include/SFML/Audio/Sound.hpp:69:5: note: sf::Sound::Sound(const sf::Sound&)
/usr/local/include/SFML/Audio/Sound.hpp:69:5: note: candidate expects 1 argument, 2 provided
/usr/local/include/SFML/Audio/Sound.hpp:61:14: note: sf::Sound::Sound(const sf::SoundBuffer&)
/usr/local/include/SFML/Audio/Sound.hpp:61:14: note: candidate expects 1 argument, 2 provided
/usr/local/include/SFML/Audio/Sound.hpp:53:5: note: sf::Sound::Sound()
/usr/local/include/SFML/Audio/Sound.hpp:53:5: note: candidate expects 0 arguments, 2 provided
audio/player.cpp:64:20: error: 'class sf::Sound' has no member named 'Play'
audio/player.cpp: In member function 'virtual bool oci::audio::{anonymous}::ControllerImpl::IsPlaying() const':
audio/player.cpp:68:23: error: 'const class sf::Sound' has no member named 'GetStatus'
audio/player.cpp: At global scope:
audio/player.cpp:77:1: error: 'shared_ptr' in namespace 'std' does not name a type
scons: *** [audio/player.o] Error 1
scons: building terminated because of errors.
The code has been updated last time in 2015, so I assume it is using some older SFML version (which i don't know) and in the latest version some functions have been changed and the compatibility broke. I can't test older versions because there is only one version available for RPi3 (2.4) .
audio/player.cpp file:
#include "player.h"
#ifdef USE_SFML
# include <SFML/Audio/Sound.hpp>
# include <SFML/Audio/SoundBuffer.hpp>
#else
# include <SDL2/SDL_mixer.h>
#endif
#include <portability/cpp11.h>
#include <resources/loader.h>
#include <stdexcept>
#include <utils/cache.h>
namespace oci {
namespace audio {
#ifdef USE_SFML
namespace {
class Loader {
public:
sf::SoundBuffer operator()(const std::string& name) {
std::vector<char> data =
resources::ResourcesLoader::Instance().GetData("sfx/" + name);
if(data.empty())
throw std::logic_error("Sound resource \"sfx/" + name +
"\" is empty");
sf::SoundBuffer sb;
if(!sb.LoadFromMemory(&data[0], data.size()))
throw std::logic_error("Cannot load sound \"" + name + "\"");
return sb;
}
};
class ControllerImpl : public Controller {
public:
ControllerImpl(const sf::SoundBuffer& sb, bool autoplay, bool loop) :
mSound(sb, loop)
{
if(autoplay)
mSound.Play();
}
virtual bool IsPlaying() const override {
return mSound.GetStatus() == sf::Sound::Playing;
}
private:
sf::Sound mSound;
};
} // namespace
std::shared_ptr<Controller> Play(const std::string& name, bool autoplay,
bool loop) {
static Cache<sf::SoundBuffer, Loader> cache;
const sf::SoundBuffer& sb = cache.Get(name);
return std::make_shared<ControllerImpl>(sb, autoplay, loop);
}
#else
// SDL audio code, not needed
#endif
} // namespace audio
} // namespace oci
Consider the following (buggy) C++ code:
#include <cmath>
#include <cstdlib>
#include <iostream>
int main() {
if (abs(-0.75) != 0.75) {
std::cout << "Math is broken!\n";
return 1;
} else {
return 0;
}
}
This code is buggy because it calls abs (meaning ::abs) instead of std::abs. Depending on the implementation, ::abs might not exist, or it might be the C abs, or it might be an overload set including a version for double, like std::abs is.
With Clang on Linux, at least in my environment, it turns out to be the second option: C abs. This provokes two warnings, even without explicitly enabling any:
<source>:7:9: warning: using integer absolute value function 'abs' when argument is of floating point type [-Wabsolute-value]
if (abs(-0.75) != 0.75) {
^
<source>:7:9: note: use function 'std::abs' instead
if (abs(-0.75) != 0.75) {
^~~
std::abs
<source>:7:13: warning: implicit conversion from 'double' to 'int' changes value from -0.75 to 0 [-Wliteral-conversion]
if (abs(-0.75) != 0.75) {
~~~ ^~~~~
On GCC, I get different results in different environments and I haven’t yet figured out what details of the environment are relevant. The more common option, though, is also that it calls the C abs function. However, even with -Wall -Wextra -pedantic, it gives no warnings. I can force a warning with -Wfloat-conversion, but that gives too many false positives on the rest of my codebase (which perhaps I should fix, but that’s a different issue):
<source>: In function 'int main()':
<source>:7:18: warning: conversion to 'int' alters 'double' constant value [-Wfloat-conversion]
if (abs(-0.75) != 0.75) {
^
Is there a way to get a warning whenever I use a library function through the global namespace, when the version in namespace std is an overload?
Here's a solution. I'm not happy with it, but it might work for you:
namespace DontUseGlobalNameSpace {
// put all std functions here you want to catch
int abs(int x);
}
using namespace DontUseGlobalNameSpace;
Now, if you use abs() without qualification, you'll get a "symbol is ambiguous" error.
This is going to be difficult. The GCC <cmath> header simply includes <math.h>, #undefs its macros (just in case) and defines the C++ functions as inline functions which make some use of identifiers from <math.h>. Most of the functions in fact refer to compiler builtins: for instance, std::abs is defined using __builtin_abs and not ::abs.
Since <cmath> and your "buggy program" are all in the same translation unit, it's hard to see how the visibility could be separated: how the inline functions in <cmath> could be allowed to use <math.h> stuff, while your code wouldn't.
Well, there is the following way: <cmath> would have to be rewritten to provide its own locally scoped declarations for anything that it needs from <math.h> and not actually include that header.
What we can do instead is prepare a header file which re-declares the functions we don't want, with an __attribute__ ((deprecated)):
// put the following and lots of others like it in a header:
extern "C" int abs(int) throw () __attribute__ ((deprecated));
#include <cmath>
#include <cstdlib>
#include <iostream>
int main() {
if (abs(-0.75) != 0.75) {
std::cout << "Math is broken!\n";
return 1;
} else {
return 0;
}
}
Now:
$ g++ -Wall buggy.cc
buggy.cc: In function ‘int main()’:
buggy.cc:9:7: warning: ‘int abs(int)’ is deprecated [-Wdeprecated-declarations]
if (abs(-0.75) != 0.75) {
^~~
In file included from /usr/include/c++/6/cstdlib:75:0,
from buggy.cc:4:
/usr/include/stdlib.h:735:12: note: declared here
extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
^~~
buggy.cc:9:16: warning: ‘int abs(int)’ is deprecated [-Wdeprecated-declarations]
if (abs(-0.75) != 0.75) {
^
In file included from /usr/include/c++/6/cstdlib:75:0,
from buggy.cc:4:
/usr/include/stdlib.h:735:12: note: declared here
extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
^~~
A linker warning would be simpler. I tried that; the problem is that this test program doesn't actually generate an external reference to abs (even though there is an #undef abs in <cmath>). The call is being inlined, and so evades the linker warning.
Update:
Following up DanielH's comment, I have come up with a refinement of the trick which allows std::abs but blocks abs:
#include <cmath>
#include <cstdlib>
#include <iostream>
namespace proj {
// shadowing declaration
int abs(int) __attribute__ ((deprecated));
int fun() {
if (abs(-0.75) != 0.75) {
std::cout << "Math is broken!\n";
return 1;
} else {
return std::abs(-1); // must be allowed
}
}
}
int main() {
return proj::fun();
}
Simple namespaces can be used. Also, we don't need the deprecated attribute; we can just declare abs as an incompatible function, or a non-function identifier entirely:
#include <cmath>
#include <cstdlib>
#include <iostream>
namespace proj {
// shadowing declaration
class abs;
int fun() {
if (abs(-0.75) != 0.75) {
std::cout << "Math is broken!\n";
return 1;
} else {
return std::abs(-1); // must be allowed
}
}
}
int main() {
return proj::fun();
}
$ g++ -std=c++98 -Wall buggy.cc -o buggy
buggy.cc: In function ‘int proj::fun()’:
buggy.cc:10:18: error: invalid use of incomplete type ‘class proj::abs’
if (abs(-0.75) != 0.75) {
^
buggy.cc:7:9: note: forward declaration of ‘class proj::abs’
class abs;
^~~
buggy.cc:16:3: warning: control reaches end of non-void function [-Wreturn-type]
}
^
With this approach, we just need a list of names and dump them into some header that provides this:
int abs, fabs, ...; // shadow all of these as non-functions
I used -stdc++98 in the g++ command line to emphasizes that this is just old school C++ namespace semantics at work.
This code will let you detect whether the trap exists in a particular environment:
double (*)(double) = &::abs; // fails if you haven't included math.h, possibly via cmath
But it won't help you spot the places you fall into the trap.
I am trying to build a sample C++ Circular buffer code but keep running into the issue of declaration of anonymous class must be a definition
The sad part is that I was able to run this code earlier on my Mac but can't do so now, can you suggest can be done to find the root cause.
Compilation
g++ temp.cpp
In file included from temp.cpp:1:
In file included from /usr/local/include/boost/circular_buffer.hpp:55:
/usr/local/include/boost/circular_buffer/base.hpp:72:1: error: declaration of anonymous class must be a definition
class <int>
^
temp.cpp:9:35: error: implicit instantiation of undefined template 'boost::circular_buffer<int, std::__1::allocator<int> >'
boost::circular_buffer<int> cb(3);
^
/usr/local/include/boost/circular_buffer_fwd.hpp:34:7: note: template is declared here
class circular_buffer;
^
2 errors generated.
CODE
#include <boost/circular_buffer.hpp>
#include <numeric>
#include <assert.h>
int main(int /*argc*/, char* /*argv*/[])
{
// create a circular buffer of capacity 3
boost::circular_buffer<int> cb(3);
// insert some elements into the circular buffer
cb.push_back(1);
cb.push_back(2);
// assertions
assert(cb[0] == 1);
assert(cb[1] == 2);
assert(!cb.full());
assert(cb.size() == 2);
assert(cb.capacity() == 3);
// insert some other elements
cb.push_back(3);
cb.push_back(4);
// evaluate the sum
int sum = std::accumulate(cb.begin(), cb.end(), 0);
// assertions
assert(cb[0] == 2);
assert(cb[1] == 3);
assert(cb[2] == 4);
assert(*cb.begin() == 2);
assert(cb.front() == 2);
assert(cb.back() == 4);
assert(sum == 9);
assert(cb.full());
assert(cb.size() == 3);
assert(cb.capacity() == 3);
return 0;
}
G++ Version
g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin16.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Boost Version is 1.63.0
=============== SOLVED ====================
It seems that for some reason the definition
template <class T, class Alloc>
class circular_buffer // circular_buffer/base.hpp line 72
/*! \cond */
#if BOOST_CB_ENABLE_DEBUG
: public cb_details::debug_iterator_registry
#endif
/*! \endcond */
{ /* ... */ };
is corrupted. Presumably, you have some macros defined, such as
#define circular_buffer <int>
I tried to install the external library SVL, that is given in the link. I did make install and it seems now I can import it as given in documentation using #include <svl/some_header_files.h>
Now I want to run some of my program to test. But I am struggling with it. I did
g++ vertex.hh vertex.cc
the program I want to work and it gives out,
In file included from vertex.hh:9:
In file included from /usr/local/include/svl/Vec3.h:14:
/usr/local/include/svl/Vec2.h:25:10: error: unknown type name 'Real'
Vec2(Real x, Real y); // (x, y)
^
/usr/local/include/svl/Vec2.h:25:18: error: unknown type name 'Real'
Vec2(Real x, Real y); // (x, y)
^
/usr/local/include/svl/Vec2.h:27:10: error: unknown type name 'ZeroOrOne'
Vec2(ZeroOrOne k); // v[i] = vl_zero
^
/usr/local/include/svl/Vec2.h:28:10: error: unknown type name 'Axis'
Vec2(Axis k); // v[k] = 1
^
/usr/local/include/svl/Vec2.h:32:5: error: unknown type name 'Real'
Real &operator [] (Int i);
^
/usr/local/include/svl/Vec2.h:32:31: error: unknown type name 'Int'; did you mean 'int'?
Real &operator [] (Int i);
and more similar errors, which is then followed by
In file included from vertex.cc:10:
In file included from /usr/local/include/svl/Vec3.h:14:
/usr/local/include/svl/Vec2.h:25:10: error: unknown type name 'Real'
Vec2(Real x, Real y); // (x, y)
^
/usr/local/include/svl/Vec2.h:25:18: error: unknown type name 'Real'
Vec2(Real x, Real y); // (x, y)
^
/usr/local/include/svl/Vec2.h:27:10: error: unknown type name 'ZeroOrOne'
Vec2(ZeroOrOne k); // v[i] = vl_zero
^
and more similar errors,
and in the end,
/usr/local/include/svl/Vec2.h:69:27: error: unknown type name 'Int'; did you mean 'int'?
Vec2 &MakeUnit(Int i, Real k = vl_one); // I[i]
^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
Can you tell me what is going on here? How can i fix this?
Here is the link to the two files vertex.ccand vertex.hh
https://www.dropbox.com/sh/4cax5ftk2lssots/AAA62m2VnSZXqBfB65VfVqFTa?dl=0
New error using #include "svl/SVL.h"
In file included from vertex.hh:13:
./edge.hh:289:10: warning: unelaborated friend declaration is a C++11 extension; specify 'class' to befriend 'QuadEdge'
[-Wc++11-extensions]
friend QuadEdge;
^
class
1 warning generated.
In file included from vertex.cc:12:
In file included from ./cell.hh:9:
./edge.hh:289:10: warning: unelaborated friend declaration is a C++11 extension; specify 'class' to befriend 'QuadEdge'
[-Wc++11-extensions]
friend QuadEdge;
^
class
In file included from vertex.cc:12:
./cell.hh:293:10: warning: unelaborated friend declaration is a C++11 extension; specify 'class' to befriend 'CellVertexIterator'
[-Wc++11-extensions]
Why are you compiling vertex.hh? Why not just
g++ vertex.cc
Secondly, can you show us the contents of your header file and source file. Actually, the source file can probably be:
#include "vertex.hh"
int main() { return 0; }
and the header file:
#include "svl/SVL.h"
Secondly, when you say "#include " are you just including some of SVL internal headers? That may not work. http://www.cs.cmu.edu/~ajw/doc/svl.html says "for basic use, the only header file needed is svl/SVL.h".
I am having trouble with the g++ compiler. On my work machine (running OS X 10.10.4) I was experimenting with some code using Xcode. The code did compile succesfully, and the resulting executable works as expected. Output of clang++ --version:
Apple LLVM version 6.1.0 (clang-602.0.53) (based on
LLVM 3.6.0svn) Target: x86_64-apple-darwin14.4.0 Thread model: posix
Then I decided to compile this code on a server running Debian 8 with g++. The output of g++ --version:
g++ (Debian 4.9.2-22) 4.9.2 Copyright (C) 2014 Free Software
Foundation, Inc.
The code won't even compile using g++. The command I tried using: g++ -std=c++11 -pthread main.cpp
I get the following error messages:
main.cpp: In function 'int main()':
main.cpp:32:106: error: invalid use of incomplete type 'class std::packaged_task'
std::shared_ptr > ptr(new std::packaged_task(std::bind(factorial, 6)));
In file included from main.cpp:11:0:
/usr/include/c++/4.9/future:120:11: error: declaration of 'class std::packaged_task'
class packaged_task;
^
main.cpp:33:22: error: variable 'std::future fu1' has initializer but incomplete type
std::future fu1 = ptr->get_future();
^
main.cpp:33:31: error: invalid use of incomplete type 'class std::packaged_task'
std::future fu1 = ptr->get_future();
^
In file included from main.cpp:11:0:
/usr/include/c++/4.9/future:120:11: error: declaration of 'class std::packaged_task'
class packaged_task;
^
main.cpp: In lambda function:
main.cpp:34:48: error: invalid use of incomplete type 'class std::packaged_task'
std::function task1 = &ptr{ ptr->operator()(); };
^
In file included from main.cpp:11:0:
/usr/include/c++/4.9/future:120:11: error: declaration of 'class std::packaged_task'
class packaged_task;
^
main.cpp: In function 'int main()':
main.cpp:36:38: error: variable 'std::packaged_task t' has initializer but incomplete type
std::packaged_task t(std::bind(factorial, 5));
^
main.cpp:37:22: error: variable 'std::future fu2' has initializer but incomplete type
std::future fu2 = t.get_future();
^
My code:
#include <iostream>
#include <thread>
#include <future>
#include <memory>
using std::cout;
using std::cin;
using std::endl;
unsigned long long int factorial(unsigned long long int num)
{
unsigned long long int N = num;
for (unsigned long long int i = num; i > 1; --i)
{
num *=(--N);
}
return num;
}
int main()
{
std::shared_ptr<std::packaged_task<int()> > ptr(new std::packaged_task<int()>(std::bind(factorial, 6)));
std::future<int> fu1 = ptr->get_future();
std::function<void()> task1 = [&ptr](){ ptr->operator()(); };
std::packaged_task<int()> t(std::bind(factorial, 5));
std::future<int> fu2 = t.get_future();
std::function<void()> task2 = [&t](){ t(); };
std::thread threads[2];
threads[0] = std::thread(task1);
threads[1] = std::thread(task2);
cout << fu1.get() << endl;
cout << fu2.get() << endl;
threads[0].join();
threads[1].join();
return 0;
}
What could be the issue with g++?
It seems, that std::future & std::async are not implemented on the armel architecture for some reason.
I can't really find out why is this (some argue on mailing lists, that they are not implemented by design, some others say this is a bug) and what is the current state of the problem.
However, I've also found a reply that stated this may be already resolved in the newer versions of libstdc++(My system is running the Testing version of debian, I do not have these versions yet, and I don't plan to get the package from unstable repos, so I'll just wait for it).