The following code fails to compile:
#include <cstdio>
#include <sstream>
int main()
{
std::ostrstream strm;
strm.rdbuf()->freeze(0);
}
I get the following errors on compilation:
g++ sample3.cpp
sample3.cpp: In function 'int main()':
sample3.cpp:5: error: 'ostrstream' is not a member of 'std'
sample3.cpp:5: error: expected `;' before 'strm'
sample3.cpp:6: error: 'strm' was not declared in this scope
After searching in google, I suspect that I should use ostringstream in place of ostrstream, so I have modified the program as below:
#include <cstdio>
#include <sstream>
int main()
{
std::ostringstream strm;
strm.rdbuf()->freeze(0);
}
But now I get the following errors:
g++ sample3.cpp
sample3.cpp: In function 'int main()':
sample3.cpp:6: error: 'struct std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >' has no member named 'freeze'
Just scrap the freeze() call -- the current generation std::ostringstream doesn't expose its memory management guts to you like the old ostrstream did. You'll need to rework your original code to let the stringstream manage memory in the way it wants (it'll be much simpler/less error-prone that way!).
I should have changed #include "sstream" to #include "strstream"
then it will not report the error "'ostrstream' is not a member of 'std'".
Related
I'm just now getting into OpenCV (4.1.0) in C++ (relatively new to both) and I'm getting an odd error from simply including some header files. (GCC 6.3.0)
Error:
from c:\Users\Logan\Projects\Code\C++\webcamTest.cpp:1:
C:\Users\Logan\Projects\Code\C++\Includes\opencv\build\include/opencv2/core/utility.hpp:697:14: error: 'recursive_mutex' in namespace 'std' does not name a type
typedef std::recursive_mutex Mutex;
^~~~~~~~~~~~~~~
C:\Users\Logan\Projects\Code\C++\Includes\opencv\build\include/opencv2/core/utility.hpp:698:25: error: 'Mutex' is not a member of 'cv'
typedef std::lock_guard<cv::Mutex> AutoLock;
^~
C:\Users\Logan\Projects\Code\C++\Includes\opencv\build\include/opencv2/core/utility.hpp:698:25: error: 'Mutex' is not a member of 'cv'
C:\Users\Logan\Projects\Code\C++\Includes\opencv\build\include/opencv2/core/utility.hpp:698:34: error: template argument 1 is invalid
typedef std::lock_guard<cv::Mutex> AutoLock;
Code:
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
int main()
{
return 0;
}
To me this looks like an error with the way the headers are set up, but perhaps I didn't set something up correctly?
Currently the command I'm using is as follows:
g++ -std=c++11 -g -I C:\\Users\\Logan\\Projects\\Code\\C++\\Includes\\opencv\\build\\include -o C:\\Users\\Logan\\Projects\\Code\\C++\\Bins\\<File name I'm using in the editor>.exe <File I'm using in the editor>
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'm learning C++ and I'm at the point of using the <algorithm> header and I get this compiler error even with an empty project:
/Users/italrolando/hpc-gcc47/bin/../lib/gcc/x86_64-apple-darwin11.4.0/4.7.1/../../../../include/c++/4.7.1/cmath:1108:11: error: '::llrint' has not been declared
/Users/italrolando/hpc-gcc47/bin/../lib/gcc/x86_64-apple-darwin11.4.0/4.7.1/../../../../include/c++/4.7.1/cmath:1109:11: error: '::llrintf' has not been declared
/Users/italrolando/hpc-gcc47/bin/../lib/gcc/x86_64-apple-darwin11.4.0/4.7.1/../../../../include/c++/4.7.1/cmath:1110:11: error: '::llrintl' has not been declared
/Users/italrolando/hpc-gcc47/bin/../lib/gcc/x86_64-apple-darwin11.4.0/4.7.1/../../../../include/c++/4.7.1/cmath:1112:11: error: '::llround' has not been declared
/Users/italrolando/hpc-gcc47/bin/../lib/gcc/x86_64-apple-darwin11.4.0/4.7.1/../../../../include/c++/4.7.1/cmath:1113:11: error: '::llroundf' has not been declared
/Users/italrolando/hpc-gcc47/bin/../lib/gcc/x86_64-apple-darwin11.4.0/4.7.1/../../../../include/c++/4.7.1/cmath:1114:11: error: '::llroundl' has not been declared
make: *** [algorithm.o] Error 1
Here is the code:
#include <iostream>
#include <algorithm>
using namespace std;
int main( int argc, char ** argv ){
return 0;
}
I'm using Eclipse with GCC 4.7.1 compiler and it's quite strange since the course I'm following is using Eclipse with Gcc 4.7 and it works.
Thanks
I am trying to write a simple program that uses freetype.
code :
#include <ft2build.h>
#include FT_FREETYPE_H
int main()
{
FT_Library library;
int error = FI_Init_FreeType(&library);
}
i am compiling as g++ try1_c++demo.cpp -I/usr/include/freetype2
it gives following error:
try1_c++demo.cpp: In function ‘int main()’:
try1_c++demo.cpp:9: error: ‘FI_Init_FreeType’ was not declared in this scope
how do i solve this error?
I have a strange build problem.
I have a simple test program that sends a sigqueue to another process.
This little code example builds and runs when I build it as a c++ program (compiled with g++)
but when I compile it as a c program (with gcc) I get a error that he can't find the sigval struct.
The short example:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
int main(int argc, char *argv[])
{
sigval value;
value.sival_int = 123;
sigqueue(0,SIGUSR1, value);
}
Please note that I replaced the pid with 0 to simplify this question.
And if I compile with gcc I get this:
$> gcc sigusr1_mini.c
sigusr1_mini.c: In function ‘main’:
sigusr1_mini.c:9: error: ‘sigval’ undeclared (first use in this function)
sigusr1_mini.c:9: error: (Each undeclared identifier is reported only once
sigusr1_mini.c:9: error: for each function it appears in.)
sigusr1_mini.c:9: error: expected ‘;’ before ‘value’
sigusr1_mini.c:10: error: ‘value’ undeclared (first use in this function)
What am I missing here, why can't he find the sigval struct?
And why can g++ find it?
Thanks
Johan
In C, struct and union tags do not introduce names that can be used on their own like they do in C++. You must spell it out:
union sigval value;
How is sigval defined in h-file? C compiler may require full definition, for example:
union sigval value;