When I use the make command, it prompts the following statement.
main. cpp: 10:9: Error: 'SIGPIPE' has not been declared in this scope
signal(SIGPIPE, SIG_IGN);
^
main. cpp: 10:18: Error: 'SIG_ IGN 'has not been declared in this scope
signal(SIGPIPE, SIG_IGN);
^
main. cpp: 10:25: Error: 'signal 'has not been declared in this scope
signal(SIGPIPE, SIG_IGN);
^
This is the code for main.cpp.
#include <signal.h>
int main(int argc, char* argv[])
{
signal(SIGPIPE, SIG_IGN);
return 0;
}
So what causes this problem?
Related
i'm using boost::asio, i want to know why there is error when i use different overload;
```
#include<boost/asio.hpp>
using namespace boost::asio;
int main(int argc,char* argv[]){
io_service ios;
ip::tcp::acceptor acc(ios);
}
```
it can run when i use the command"g++ -o server server.cpp -lboost_system -lboost_thread -lpthread"
there is another situation:
```
#include<boost/asio.hpp>
using namespace boost::asio;
int main(int argc,char* argv[]){
io_service ios();
ip::tcp::acceptor acc(ios);
}
```
it cant run and there is the errorinfo:
In file included from /usr/include/boost/asio/executor.hpp:338,
from /usr/include/boost/asio/basic_socket.hpp:27,
from /usr/include/boost/asio/basic_datagram_socket.hpp:20,
from /usr/include/boost/asio.hpp:24,
from server.cpp:1:
/usr/include/boost/asio/impl/executor.hpp: In instantiation of ‘boost::asio::execution_context& boost::asio::executor::impl< <template-parameter-1-1>, <template-parameter-1-2> >::context() [with Executor = boost::asio::io_context (*)(); Allocator = std::allocator<void>]’:
/usr/include/boost/asio/impl/executor.hpp:177:22: required from here
/usr/include/boost/asio/impl/executor.hpp:179:22: error: request for member ‘context’ in ‘((boost::asio::executor::impl<boost::asio::io_context (*)(), std::allocator<void> >*)this)->boost::asio::executor::impl<boost::asio::io_context (*)(), std::allocator<void> >::executor_’, which is of non-class type ‘boost::asio::io_context (*)()’
179 | return executor_.context();
| ~~~~~~~~~~^~~~~~~
io_service ios();
This declares a function called ios returning io_service. This is known as the "Most Vexing Parse".
Either use {} instead of () (since C++11) or just leave the parentheses.
I am trying to initialize struct sigaction (from sigaction.h) using C++20 designated initializers, but there are compiler errors. As this is part of a larger program, I created a short example:
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
void onSIGINT(int signal, siginfo_t *siginfo, void *context) {
printf("Caught signal\n");
}
int main(int argc, char** argv) {
struct sigaction act = {
.sa_sigaction = onSIGINT,
.sa_flags = SA_SIGINFO
};
if(sigaction(SIGINT, &act, nullptr) != 0) {
fprintf(stderr, "Failed to listen for SIGINT\n");
return 1;
}
printf("Waiting for signal...\n");
pause();
printf("Exit\n");
return 0;
}
According to https://gcc.gnu.org/projects/cxx-status.html#cxx20 designated initializers are available in gcc8. Running g++ (v8.3.0 on Debian buster) with -std=c++2a gives the following error:
main.cpp: In function ‘int main(int, char**)’:
main.cpp:11:9: error: expected primary-expression before ‘.’ token
.sa_sigaction = onSIGINT,
^
main.cpp:12:9: error: either all initializer clauses should be designated or none of them should be
.sa_flags = SA_SIGINFO
^
Initializing only sa_flags compiles successfully, initializing only sa_sigaction fails to compile (only first error).
I also tried to initialize __sigaction_handler directly (no using the define to access the union member):
struct sigaction act = {
.__sigaction_handler = { .sa_sigaction = onSIGINT },
.sa_flags = SA_SIGINFO
};
That produces a similar error:
main.cpp: In function ‘int main(int, char**)’:
main.cpp:12:34: error: expected primary-expression before ‘.’ token
.__sigaction_handler = { .sa_sigaction = onSIGINT },
^
I suppose I am doing something wrong about the union inside the struct, but I can't figure out what.
I am aware that I could achieve roughly the same by zeroing the memory of the struct and then setting callback and flags, but thats not the point.
The problem is that sa_sigaction is a macro, defined to __sigaction_handler.sa_sigaction. This means that your code expands to .__sigaction_handler.sa_sigaction = onSIGINT; this is valid C, where a named member initializer is allowed to have a complex structure, but it is not valid in C++20.
You can see that by #undefing it:
#undef sa_sigaction
.__sigaction_handler = { .sa_sigaction = onSIGINT },
However this is non-portable (undefining a standard library macro, not to mention using a double-underscore prefixed member), so I would recommend against it.
I'm looking up c++ library, and see the istream class, I am confused with a contractor with an address symbol. what is the meaning of a constructor with an address symbol?
one of the istream constructors is.
protected: iostream& (iostream&& x);
I found it in website cplusplus.com,
link: iostream
I defined a customer class with a similar constructor that has a & symbol:
//Test.cpp
#include <iostream>/*cout,cin*/
#include <typeinfo>/*typeid(),name()*/
using namespace std;
struct MyTest{
MyTest&(double b){}
};
int main(int argc,char* argv[]){
MyTest mt2(2.1);
cout << typeid(mt2).name() << endl;
return 0;
}
I use the below command to compile it:
g++ Test.cpp -o Test -std=c++11
however, I get some compile error messages:
Test.cpp:7:11: error: expected unqualified-id before ‘float’
MyTest&(float b){}
^
Test.cpp:7:11: error: expected ‘)’ before ‘float’
Test.cpp:7:10: error: expected ‘;’ at end of member declaration
MyTest&(float b){}
^
Test.cpp:7:17: error: expected ‘;’ at end of member declaration
MyTest&(float b){}
^
Test.cpp:7:18: error: expected unqualified-id before ‘)’ token
MyTest&(float b){}
^
Test.cpp: In function ‘int main(int, char**)’:
Test.cpp:12:16: error: no matching function for call to ‘MyTest::MyTest(double)’
MyTest mt2(2.1);
I got confused, c++ library istream class is fine. why did my custom class constructor fail? what am I missing?
The information on cplusplus.com is... sometimes not dependable. (See What's wrong with cplusplus.com? for a discussion of this.) On CPPReference, you can see that the move constructor is, you know, just a regular move constructor.
This is a bug in http://www.cplusplus.com/reference/istream/iostream/iostream/.
If you look at https://en.cppreference.com/w/cpp/io/basic_iostream/basic_iostream, you will find
protected: basic_iostream( basic_iostream&& other );
I've got this code snippet trying to use gtest Debug log:
#include <gtest/gtest.h>
#include <iostream>
using namespace std;
int DieInDebugElse1(int* sideeffect) {
if (sideeffect) *sideeffect = 1;
#ifndef NDEBUG
GTEST_LOG_(FATAL)<<"debug death inside DieInDebugElse12()";
#endif // NDEBUG
return 12;
}
TEST(a,c){
int sideeffect = 0;
EXPECT_DEBUG_DEATH(DieInDebugElse1(&sideeffect), "death");
}
Compile error shows:
error: ‘FATAL’ was not declared in this scope
GTEST_LOG(FATAL, "debug death inside DieInDebugElse12()");
^~~~~
m.cpp:57:15: note: suggested alternative: ‘FAIL’
GTEST_LOG(FATAL, "debug death inside DieInDebugElse12()");
^~~~~
FAIL
m.cpp:57:5: error: ‘GTEST_LOG’ was not declared in this scope
GTEST_LOG(FATAL, "debug death inside DieInDebugElse12()");
^~~~~~~~~
m.cpp:57:5: note: suggested alternative: ‘GTEST_LOG_’
GTEST_LOG(FATAL, "debug death inside DieInDebugElse12()");
^~~~~~~~~
GTEST_LOG_
scons: *** [m.o] Error 1
How to fix it?
I am learning TDD, using GoogleTest framework. I have successfully built Gtest and have been able to build and run the samples. However, when I tried a simple sample I wrote, I am getting compilation errors.
Here is the source and the build commands I used:
// ################################################
//proj1.h
#ifndef __SCRATCH_PROJ1_H
#define __SCRATCH_PROJ1_H
int addOne(int i);
#endif /*__SCRATCH_PROJ1_H */
// ################################################
//proj1.cpp
#include "proj1.h"
int addOne(int i){
return i+1;
}
// ################################################
//proj1_unittest.cpp
#include "proj1.h"
#include "gtest/gtest.h"
// Test Function
TEST(addOneTest, Positive) {
EXPECT_EQ(1,addOneTest(0)); // <- Line # 24
EXPECT_EQ(2,addOneTest(1)); // <- Line # 25
EXPECT_EQ(40320, addOneTest(40319)); // <- Line # 26
}
TEST(addOneTest, Negative) {
EXPECT_FALSE(addOneTest(-1)); // <- Line # 30
}
GTEST_API_ int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Console output:
g++ -isystem ${GTEST_DIR}/include -pthread -c
/home/user1/scratch/proj1_unittest.cpp
/home/user1/scratch/proj1_unittest.cpp: In member function ‘virtual
void addOneTest_Positive_Test::TestBody()’:
/home/user1/scratch/proj1_unittest.cpp:24:5: error: ‘addOneTest’ was
not declared in this scope
/home/user1/scratch/proj1_unittest.cpp:25:5: error: ‘addOneTest’ was
not declared in this scope
/home/user1/scratch/proj1_unittest.cpp:26:5: error: ‘addOneTest’ was
not declared in this scope /home/user1/scratch/proj1_unittest.cpp: In
member function ‘virtual void addOneTest_Negative_Test::TestBody()’:
/home/user1/scratch/proj1_unittest.cpp:30:5: error: ‘addOneTest’ was
not declared in this scope
Judging from the line numbers in the error messages, it seems that the EXPECT_* macros have not been defined - BUT, I have included gtest/gtest.h in the compilation unit.
What is causing these errors - and how do I fix it?
As it says, addOneTest was not declared anywhere. I'm guessing you meant to call addOne instead.