We have a project that uses a wrapper for Boost socket. I'm trying to add a wrapper for the sent_to() command, but can't get it to compile. We're using C++11.
In the header file, this:
template<typename ConstBufferSequence>
std::size_t send_to(
const ConstBufferSequence & buffers,
const endpoint_type & destination, // error
socket_base::message_flags flags, // error
boost::system::error_code & ec);
generates two errors. They are:
"Type 'endpoint_type' could not be resolved", and
"Type 'socket_base::message_flags' could not be resolved
I've included the requisite header file, and even a more direct one:
#include <boost/asio.hpp>
#include <boost/asio/basic_datagram_socket.hpp>
Neither resolve the issues. I'm sure there is some template black magic that needs to be done here, but I'm at a loss as to what it is. Can anyone give me a pointer as to what's going wrong here?
Related
When I try to declare a c++ vector, and initialize it at the same time, I get an error. This only happens to me in codelite - the same exact code works in xcode.
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector <int> myvec {32, 234, 25, 235};
cout<<myvec[1];
return 0;
}
The error is "expected ';' at end of declarion." Then, it shows an arrow pointing in between "myvec" and "{32,234....};", and says to put a ';' there. I know this sounds a little unclear - I can provide a screenshot if necessary.
Thanks for the help.
I'm aware of many similar questions on this site. I really like the solution mention in the following link:
https://stackoverflow.com/a/25021520/884553
with some modification, you can include text file at compile time, for example:
constexpr const char* s =
#include "file.txt"
BUT to make this work you have to add string literal prefix and suffix to your original file, for example
R"(
This is the original content,
and I don't want this file to be modified. but i
don't know how to do it.
)";
My question is: is there a way to make this work but not modifying file.txt?
(I know I can use command line tools to make a copy, prepend and append to the copy, remove the copy after compile. I'm looking for a more elegant solution than this. hopefully no need of other tools)
Here's what I've tried (but not working):
#include <iostream>
int main() {
constexpr const char* s =
#include "bra.txt" // R"(
#include "file.txt" //original file without R"( and )";
#include "ket.txt" // )";
std::cout << s << "\n";
return 0;
}
/opt/gcc8/bin/g++ -std=c++1z a.cpp
In file included from a.cpp:5:
bra.txt:1:1: error: unterminated raw string
R"(
^
a.cpp: In function ‘int main()’:
a.cpp:4:27: error: expected primary-expression at end of input
constexpr const char* s =
^
a.cpp:4:27: error: expected ‘}’ at end of input
a.cpp:3:12: note: to match this ‘{’
int main() {
^
No, this cannot be done.
There is a c++2a proposal to allow inclusion of such resources at compile time called std::embed.
The motivation part of ths p1040r1 proposal:
Motivation
Every C and C++ programmer -- at some point -- attempts to #include large chunks of non-C++ data into their code. Of course, #include expects the format of the data to be source code, and thusly the program fails with spectacular lexer errors. Thusly, many different tools and practices were adapted to handle this, as far back as 1995 with the xxd tool. Many industries need such functionality, including (but hardly limited to):
Financial Development
representing coefficients and numeric constants for performance-critical algorithms;
Game Development
assets that do not change at runtime, such as icons, fixed textures and other data
Shader and scripting code;
Embedded Development
storing large chunks of binary, such as firmware, in a well-compressed format
placing data in memory on chips and systems that do not have an operating system or file system;
Application Development
compressed binary blobs representing data
non-C++ script code that is not changed at runtime; and
Server Development
configuration parameters which are known at build-time and are baked in to set limits and give compile-time information to tweak performance under certain loads
SSL/TLS Certificates hard-coded into your executable (requiring a rebuild and potential authorization before deploying new certificates).
In the pursuit of this goal, these tools have proven to have inadequacies and contribute poorly to the C++ development cycle as it continues to scale up for larger and better low-end devices and high-performance machines, bogging developers down with menial build tasks and trying to cover-up disappointing differences between platforms.
MongoDB has been kind enough to share some of their code below. Other companies have had their example code anonymized or simply not included directly out of shame for the things they need to do to support their workflows. The author thanks MongoDB for their courage and their support for std::embed.
The request for some form of #include_string or similar dates back quite a long time, with one of the oldest stack overflow questions asked-and-answered about it dating back nearly 10 years. Predating even that is a plethora of mailing list posts and forum posts asking how to get script code and other things that are not likely to change into the binary.
This paper proposes <embed> to make this process much more efficient, portable, and streamlined. Here’s an example of the ideal:
#include <embed>
int main (int, char*[]) {
constexpr std::span<const std::byte> fxaa_binary = std::embed( "fxaa.spirv" );
// assert this is a SPIRV file, compile-time
static_assert( fxaa_binary[0] == 0x03 && fxaa_binary[1] == 0x02
&& fxaa_binary[2] == 0x23 && fxaa_binary[3] == 0x07
, "given wrong SPIRV data, check rebuild or check the binaries!" )
auto context = make_vulkan_context();
// data kept around and made available for binary
// to use at runtime
auto fxaa_shader = make_shader( context, fxaa_binary );
for (;;) {
// ...
// and we’re off!
// ...
}
return 0;
}
I haven't worked with C++ for several years and now I'm required to maintain a C++ project. I have the following piece of code which seems to compile in one project, but not in the other.
list.h
#include "mytype.h"
#include <set>
typedef std::set<MYTYPE> MYTYPE_LIST;
typedef MYTYPE_LIST::iterator MYTYPE_LIST_ITERATOR;
class LIST {
[...]
MYTYPE_LIST list;
};
list.cpp
void
LIST::somemethod(MYTYPE* requester)
{
MYTYPE_LIST_ITERATOR it;
for (it = list.begin(); it != list.end() ; it++ )
{
MYTYPE& info = (*it); // Error on this line
[...]
}
}
I am compiling my project with VS 2010, and I get the following errors on the marked line:
error C2440: 'initializing' : cannot convert from 'const MYTYPE' to 'MYTYPE &'
IntelliSense: qualifiers dropped in binding reference of type "MYTYPE &" to initializer of type "const MYTYPE"
I googled these errors, and as far as I understand, (*it) should not be const (because 'it' is not a ::const_iterator, see here), and therefor, I should be able to assign it to the reference variable.
In a std::set the data is the key, and since keys are constant you can't get a non-const reference.
Eventually I found that in VS 2010, Microsoft introduced a change which breaks backward compatibility with this code.
Apparently, this change was made for good reasons - modifying a set element might break the set's invariants. See here, "Problem 3: error C2662: 'NamedNumber::change_name' : cannot convert 'this' pointer from 'const NamedNumber' to 'NamedNumber &'".
The other project was compiled with and old version of Visual Studio, so we had no problem there.
I am getting the following error in my program:
error D8016: '/ZI' and '/clr' command-line options are incompatible
This happens when I put the following lines and enable common runtime in configuration->General (If I dont enable it then the error will come at using system and System::Drawing )
#using <system.drawing.dll>
using namespace System;
using namespace System::Drawing;
Actually I will be using some windows library in my code that requires the above dll.
How to solve this issue?
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/imgproc/imgproc_c.h>
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <ctype.h>
#using <system.drawing.dll>
using namespace System;
using namespace System::Drawing;
using namespace std;
int main( int argc, char** argv )
{
IplImage *source = cvLoadImage( "Image.bmp");
// Here we retrieve a percentage value to a integer
int percent =20;
// declare a destination IplImage object with correct size, depth and channels
IplImage *destination = cvCreateImage
( cvSize((int)((source->width*percent)/100) , (int)((source->height*percent)/100) ),
source->depth, source->nChannels );
//use cvResize to resize source to a destination image
cvResize(source, destination);
// save image with a name supplied with a second argument
cvShowImage("new:",destination);
cvWaitKey(0);
return 0;
}
In visual studio to turn off /ZI:
Open the project's Property Pages dialog box.
Click the C/C++ folder.
Click the General property page.
Modify the Debug Information Format property - set it to "None"
Upgrading VS will help. Minimum version: 16.11.11
In addition to what the Answer by PGP suggests, consider also changing C/C++ -> Optimization -> Optimization to Disabled (/Od).
Having it as Maximum Optimization (Favor Speed) (/O2) might give you problems when compiling for debug.
-O2 it's a certain level of compile-time optimisation. Google about what it does
In VS2017:
\ZI is set by C/C++>General>Debug Information Format = Program Database for Edit and Continue
\GL is set by C/C++>Optimization>Whole Program Optimization = Yes
I copied a configuration that I wanted to use as debug config and ran into that issue.
I have a funny situation. I am sitting on a computer on which there is a shared folder, say, "My_Folder". So, I can access it using the address "\My_Box\My_Folder".
Now, if I write a small tool in C#, I can convert the "\My_Box\My_Folder" to "C:\My_Folder" which is its physically location.
The funny thing is that I don't know how to find the physically location of "My_Box\My_Folder" without my program ... And it's actually one of my friend's problem.
So if anyone knows how to find the local path from a network path with some simple basic Windows commands/operations (either Winxp, 2000, vista, 7, ...), please let me know.
Thanks,
Pete.
If all you need is a command line tool that will provide this information then you can simply use the net share command that’s built into Windows.
If you need to do it programmatically you can use the NetShareGetInfo function. The following example shows how to use it to query the path for \\localhost\share in C++.
#include <windows.h>
#include <lm.h>
#pragma comment (lib, "netapi32.lib")
#include <iostream>
using namespace std;
int main()
{
BYTE * buffer = nullptr;
auto error = NetShareGetInfo(nullptr, // local computer
L"share",
2, // level
&buffer);
if (ERROR_SUCCESS == error)
{
auto info = reinterpret_cast<SHARE_INFO_2 *>(buffer);
wcout << info->shi2_path << endl;
}
if (buffer)
{
NetApiBufferFree(buffer);
}
}
I would wrap the buffer in a class and call NetApiBufferFree from its destructor but that’s up to you.