我有一个功能
ClassA::FuncA(const char *filePath)
Run Code Online (Sandbox Code Playgroud)
并希望将此const char字符串*复制到char*!
我的解决方案
char *argv[2];
int length = strlen(filePath);
argv[1] = new char(length +1);
strncpy(argv[1], filePath, length);
Run Code Online (Sandbox Code Playgroud)
在此之后,我在argv [1]中得到了所需的字符,还有其他一些未定义的字符!
文件路径:
"C:\用户\用户A \Parameter.xmlþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþKŸQyá•"
这里有什么不对?strlen的长度还可以!
如何确定使用 create_string_buffer 创建的缓冲区的已用大小/长度?
buffer = create_string_buffer(1000)
Run Code Online (Sandbox Code Playgroud)
例如,在缓冲区中,我有 3 个值,997 个未使用 -> 如何获得 3 个值?
在混合代码C++应用程序中捕获std和System异常的正确方法是什么?
像这样?
try
{
// using pure C++ and managed components
}
catch(std::exception exp)
{
}
catch(System::Exception sexp)
{
}
Run Code Online (Sandbox Code Playgroud) 我想知道这样的事情是可能的:
// declaration
void func();
int main()
{
int ar[] = { 1, 2, 3 };
func(ar); // call with parameter
return 1;
}
void func() // no parameters
{
// do something
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释我这个,尤其是我怎么能访问ar的func()?
我有以下问题:
我的程序应该在运行时决定加载一个函数(在本例中是GetExtendedTcpTable()),因为该方法在Windows 2000中不可用!(无法仅在Windows 2000中启动软件)
谢谢您的帮助!
招呼leon22
我有以下声明,它编译:
static unsigned char CMD[5] = {0x10,0x03,0x04,0x05,0x06};
int Class::functionA(int *buflen)
{
...
int length = sizeof(CMD); + *buflen; // compiler should cry! why not?
...
}
Run Code Online (Sandbox Code Playgroud)
为什么我没有编译错误?
我正在使用此代码将unsigned char*(指向256个值的数组)转换为std :: string:
int ClassA::Func(unsigned char *dataToSend, int sendLength)
{
std::stringstream convertStream;
std::string dataToSendStr = "";
for(int i=0; i<=sendLength; i++) convertStream << dataToSend[i];
while(!convertStream.eof()) convertStream >> dataToSendStr;
...
}
Run Code Online (Sandbox Code Playgroud)
但后来我有这种格式的dataToSendStr:
dataToSendStr ""
[0] 0x00
[1] 0x00
[2] 0x04
[3] 0xC0
Run Code Online (Sandbox Code Playgroud)
如果我现在使用这个值我只得到""而不是重要值[0-3]!
- >需要类似:dataToSendStr"000004C0"
谢谢你的帮助!
我总是得到
未定义的引用`Graph :: InsertVertex(std :: string)'
如果我编译我的项目!有什么提示他为什么不能解决这个问题?(所有文件都在netbeans项目文件夹中)
// main.cpp
#include <cstdlib>
#include <string>
#include "Graph.h"
using namespace std;
int main(int argc, char** argv)
{
Graph<string> *graph = new Graph<string>(); // <--- ERROR
graph->InsertVertex("A");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
// Node.h
#include <iostream>
#include "Graph.h"
template<class T>
class Node
{
friend class Graph;
public:
Node(T val)
{
this->data = val;
this->vertList = NULL;
this->next = NULL;
}
Node(const Node& orig);
virtual ~Node();
private:
T data;
Node<T> *vertList;
Node<T> *next;
int status;
};
Run Code Online (Sandbox Code Playgroud)
// …
目前,我已经实现了迈耶单例的这一实现:
class ClassA
{
public:
static ClassA& GetInstance()
{
static ClassA instance;
return instance;
}
private:
ClassA::ClassA() {};
// avoid copying singleton
ClassA(ClassA const&);
void operator = (ClassA const&);
};
Run Code Online (Sandbox Code Playgroud)
现在,我需要进行一些改进,以确保此代码线程在C ++-98和VS-2008中安全?
谢谢!
PS:目前尚不清楚?您会看到标签visual-studio-2008和c ++-98->,因此目标操作系统是Windows!我也不明白为什么我只被某些人完全不喜欢辛格尔顿一家的人投票!
我在一个解决方案中有 10 个 C++ 项目,SolA并且想用 GoogleTest 对它们进行单元测试:
所以我创建了一个新的解决方案SolATest,并为每个项目SolA中的单元测试项目SolATest!
加载 SolATest/Projects 中隐含的 SolA 库并将每个测试项目作为可执行文件运行是否是一种好方法:
#include <iostream>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
int main(int argc, char **argv)
{
::testing::InitGoogleMock(&argc, argv);
int value = RUN_ALL_TESTS();
std::getchar(); // to hold the terminal open
return value;
}
Run Code Online (Sandbox Code Playgroud)
或者是否有更方便的方法 -> 例如,只有一个可执行文件SolATest并将其他测试项目作为库加载(恕我直言,在一个测试项目中包含所有 cpp 文件令人困惑)?!
感谢任何帮助
什么意思是发送命令中0x000000be的结果:
iResult = send( ConnectSocket, dataToSend, (int) strlen(dataToSend), 0 );
Run Code Online (Sandbox Code Playgroud)
我没有在这里找到这个返回码:http://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v = vs.85).aspx
有任何想法吗?
谢谢
我使用来保护我的线程函数在类中,CRITICAL_SECTION并执行许多Send / Receive套接字操作,一切正常,但是如果线程正在向日志文件写入,就会遇到麻烦!
H
class ClassA
{
public:
ClassA();
~ClassA();
void run();
...
private:
CRITICAL_SECTION criticalSection;
LogFiles *m_logFiles;
...
};
Run Code Online (Sandbox Code Playgroud)
cpp
ClassA::ClassA()
{
m_logFiles = new LogFiles();
InitializeCriticalSection(&criticalSection);
}
ClassA::~ClassA()
{
delete m_logFiles;
DeleteCriticalSection(&criticalSection);
}
void ClassA::run()
{
EnterCriticalSection(&criticalSection);
// do some stuff
m_logFiles->WriteToFile(message);
// do some stuff
m_logFiles->WriteToFile(message);
LeaveCriticalSection(&criticalSection);
}
Run Code Online (Sandbox Code Playgroud)
日志文件不包含所有信息(仅包含来自例如4个线程中的2个线程的数据)或被覆盖的行(同时写入2个线程)!
所以我想我还必须确保LogFiles中的WriteToFile方法安全吗?
感谢您的帮助和/或示例!
我使用以下代码将我的类的公共属性转换为Dictionary:
public static Dictionary<string, object> ClassPropsToDictionary<T>(T classProps)
{
return classProps.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)
.ToDictionary(prop => prop.Name, prop => prop.GetValue(classProps, null));
}
Run Code Online (Sandbox Code Playgroud)
这工作正常,但我不想要类的引用成员:
public class Unit
{
public virtual string Description { get; set; } // OK
public virtual Employee EmployeeRef { get; set; } // DONT WANT THIS
}
Run Code Online (Sandbox Code Playgroud)
我需要哪个绑定标志来避免EmployeeRef成员?
谢谢
c++ ×10
winapi ×2
arrays ×1
c ×1
c# ×1
c++98 ×1
char ×1
clr ×1
ctypes ×1
dictionary ×1
exception ×1
function ×1
googletest ×1
linq ×1
loadlibrary ×1
python ×1
send ×1
singleton ×1
sockets ×1
stdstring ×1
stringstream ×1
strncpy ×1
templates ×1
unit-testing ×1