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>
Related
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)
I am using the CUB device function just like the example here (https://forums.developer.nvidia.com/t/cub-library/37675/2). I was able to compile the .cu source file in the above example using nvcc.
However, I wonder if it is possible to call CUB device function in .cpp source file and compile the .cpp source file (using nvcc or g++)? I know its possible for thrust, since the example here works for me.
Currently I simply move the main function into a new main.cpp file and include the cub header file in main.cpp, but I failed to compile it using nvcc or g++ because of the same errors, part of the error message:
/home/xx/cub/cub/block/specializations/../../block/../util_type.cuh:261:5: error: ‘__host__’ does not name a type; did you mean ‘__loff_t’?
__host__ __device__ __forceinline__ NullType& operator =(const T&) { return *this; }
^~~~~~~~
__loff_t
/home/xx/cub/cub/block/specializations/../../block/../util_type.cuh:316:19: error: ‘short4’ was not declared in this scope
__CUB_ALIGN_BYTES(short4, 8)
^
/home/xx/cub/cub/block/specializations/../../block/../util_type.cuh:314:52: error: ISO C++ forbids declaration of ‘__align__’ with no type [-fpermissive]
{ enum { ALIGN_BYTES = b }; typedef __align__(b) t Type; };
^
/home/xx/cub/cub/block/specializations/../../block/../util_type.cuh:545:9: error: ‘__host__’ does not name a type; did you mean ‘__loff_t’?
__host__ __device__ __forceinline__ CubVector operator+(const CubVector &other) const { \
^
/home/xx/cub/cub/block/specializations/../../block/../util_arch.cuh:64:38: error: ‘__host__’ does not name a type; did you mean ‘CUhostFn’?
#define CUB_RUNTIME_FUNCTION __host__ __device__
^
/home/xx/cub/cub/device/../iterator/arg_index_input_iterator.cuh:144:25: error: ‘__forceinline__’ does not name a type; did you mean ‘__thrust_forceinline__’?
__host__ __device__ __forceinline__ ArgIndexInputIterator(
^~~~~~~~~~~~~~~
__thrust_forceinline__
/home/xx/cub/cub/device/device_reduce.cuh:148:12: error: ‘cudaError_t’ does not name a type; did you mean ‘cudaError_enum’?
static cudaError_t Reduce(
^~~~~~~~~~~
cudaError_enum
Here are my source files:
device.h
#pragma once
#include <cub/cub.cuh>
void scan_on_device();
device.cu
#include "device.h"
void scan_on_device()
{
// Declare, allocate, and initialize device pointers for input and output
int num_items = 7;
int *d_in;
int h_in[] = {8, 6, 7, 5, 3, 0, 9};
int sz = sizeof(h_in)/sizeof(h_in[0]);
int *d_out; // e.g., [ , , , , , , ]
cudaMalloc(&d_in, sz*sizeof(h_in[0]));
cudaMalloc(&d_out, sz*sizeof(h_in[0]));
cudaMemcpy(d_in, h_in, sz*sizeof(h_in[0]), cudaMemcpyHostToDevice);
printf("\nInput:\n");
for (int i = 0; i < sz; i++) printf("%d ", h_in[I]);
// Determine temporary device storage requirements
void *d_temp_storage = NULL;
size_t temp_storage_bytes = 0;
cub::DeviceScan::InclusiveSum(d_temp_storage, temp_storage_bytes, d_in, d_out, num_items);
// Allocate temporary storage
cudaMalloc(&d_temp_storage, temp_storage_bytes);
// Run inclusive prefix sum
cub::DeviceScan::InclusiveSum(d_temp_storage, temp_storage_bytes, d_in, d_out, num_items);
// d_out s<-- [8, 14, 21, 26, 29, 29, 38]
cudaMemcpy(h_in, d_out, sz*sizeof(h_in[0]), cudaMemcpyDeviceToHost);
printf("\nOutput:\n");
for (int i = 0; i < sz; i++) printf("%d ", h_in[i]);
printf("\n");
}
host.cpp
#include "device.h"
#include <cub/cub.cuh>
int main(void)
{
scan_on_device();
return 0;
}
I tried to compile them in three steps:
nvcc -O2 -c device.cu -I/home/xx/cub
g++ -O2 -c host.cpp -I/usr/local/cuda/include/ -I/home/xx/cub
g++ -o tester device.o host.o -L/usr/local/cuda/lib64 -lcudart
The first step went well, but the second step gives the above errors. Any ideas are appreciated. Maybe I mess up some links (to cuda or cub)?
The cub header library (e.g. cub.cuh) contains CUDA C++ code. Such code cannot be compiled by an ordinary host compiler like g++. You will get compile errors if you try to do so.
However your project doesn't require cub.cuh to be in your device.h header file, nor does it require cub.cuh to be compiled by g++. The only thing needed in your device.h header file is the function prototype for scan_on_device().
Therefore if you include the cub header file in the function implementation file device.cu, and remove it elsewhere in your project, your code will compile.
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).
Consider this sample of code:
#include <iostream>
namespace /* unnamed namespace */
{
struct Foo
{
int a;
int b;
};
}
struct Boo
{
Foo Foo; /* field name same as field type */
int c;
void print();
};
void Boo::print()
{
std::cout<<"c = "<<c<<std::endl;
std::cout<<"Foo "<<Foo.a<<" "<<Foo.b<<std::endl;
}
int main()
{
Boo boo;
boo.c=30;
boo.Foo.a=-21;
boo.Foo.b=98;
boo.print();
return 0;
}
Clang can compile it without errors.
Debian clang version 3.5.0-9 (tags/RELEASE_350/final) (based on LLVM 3.5.0)
Microsoft cl.exe compile it without errors. (I don't remember version. I use VS 2012)
And GCC: gcc version 4.9.2 (Debian 4.9.2-10):
main.cpp:14:6: error: declaration of ‘{anonymous}::Foo Boo::Foo [-fpermissive]
Foo Foo; /* field name same as field type */
^
main.cpp:5:9: error: changes meaning of ‘Foo’ from ‘struct {anonymous}::Foo’[-fpermissive]
struct Foo
^
What is good behavior of compiler? Why GCC can't compile it, but clang and cl.exe does? What C++ standard says?
Both are correct. Per §3.3.7/1
The following rules describe the scope of names declared in classes.
[..]
A name N used in a class S shall refer to the same declaration in its context and when re-evaluated in the completed scope of S. No diagnostic is required for a violation of this rule.
Neither are obligated to give an error, gcc chose to and clang apparently chose not to. It's conforming either way.
I have to compile program probcons but there are many errors. In readme autor writes program is compatible with gcc 4.3 but I have only 4.7.2. Is there any way to compile this old program? I don't believe that in program are errors because many bioinformatics servers are using it.
Most odd to me is this error:
Description Resource Path Location Type
expected ‘)’ before ‘size’ SafeVector.h /probcons line 27 C/C++ Problem
expected ‘)’ before ‘size’ SafeVector.h /probcons line 26 C/C++ Problem
In class SafeVector.h:
/////////////////////////////////////////////////////////////////
// SafeVector.h
//
// STL vector with array bounds checking. To enable bounds
// checking, #define ENABLE_CHECKS.
/////////////////////////////////////////////////////////////////
#ifndef SAFEVECTOR_H
#define SAFEVECTOR_H
#include <cassert>
#include <vector>
/////////////////////////////////////////////////////////////////
// SafeVector
//
// Class derived from the STL std::vector for bounds checking.
/////////////////////////////////////////////////////////////////
template<class TYPE>
class SafeVector : public std::vector<TYPE>{
public:
// miscellaneous constructors
SafeVector() : std::vector<TYPE>() {}
/*ERROR HERE*/ SafeVector(size_t size) : std::vector<TYPE>(size) {}
/*ERROR HERE*/ SafeVector(size_t size, const TYPE &value) : std::vector<TYPE>(size, value) {}
SafeVector(const SafeVector &source) : std::vector<TYPE>(source) {}
#ifdef ENABLE_CHECKS
// [] array bounds checking
TYPE &operator[](int index){
assert (index >= 0 && index < (int) size());
return std::vector<TYPE>::operator[] ((size_t) index);
}
// [] const array bounds checking
const TYPE &operator[] (int index) const {
assert (index >= 0 && index < (int) size());
return std::vector<TYPE>::operator[] ((size_t) index) ;
}
#endif
};
How it is possible that in older version of gcc wasn't need includes and std::prefix?
Try specifying the language standard for gcc. I suggest you try
-std=c++11
e.g g++ -std=c++11 -o out.exe myfile.cpp
first, I think it is most likely to compile it. If it doesn't work, try other choices.
How to add this depends on how the compilation is done, but one "quick&dirty" way is to add this is to find the makefile, find a line which specifies compiler flags variable CXXFLAGS and add that to it. Note: if it is a generated makefile, edits get overwritten if you run the generator again.