I am writing a program in C++. When I use the strlen function, it is underlined by a red line. Although the project is built without errors. This is how I use this function. (By the way, strcpy is also underlined).
Exception::Exception(int _Line, char* _File, char* _Func, char* _Desc)
{
Line = _Line;
int size = strlen(_File) + 1;
File = new char[size];
strcpy(File, _File);
Func = new char[size];
strcpy(Func, _Func);
Desc = new char[size];
strcpy(Desc, _Desc);
}
And I declared <cstring> library at the beginning of the file. Please tell me how I can fix this?
As you can read up in the documentation for strlen(), strlen() is defined in string.h and the C++ counterpart std::strlen() is defined in cstring.
So to get rid of the error squiggles try adding one of the aforementioned headers.
#include <cstring>
...
std::strlen()
The system that underlines the code in the VS editor, called IntelliSense, does not use the same code as the compiler itself (or at least did not a few years ago when I used it last time). Sometimes it gets confused.
Try std::strlen instead, reorganizing the code, the includes, or something else.
Related
I'm trying to add a simple diagnostic output to a C++ UWP shared project akin to System.Diagnostics.Debug.WriteLine in C#. Following the documentation for OutputDebugString here and this solution here I've tried this:
char buf[1024];
sprintf(buf, "frequency = %f", (float)result);
OutputDebugString(buf);
but I get the compiler error
argument of type "char*" is incompatible with parameter of type "LPCWSTR"
How do I fix this?
A colleague advised me to add
#include "strsafe.h"
after any pre-compiled headers and then use this code instead
TCHAR buf[1024];
size_t cbDest = 1024 * sizeof(TCHAR);
StringCbPrintf(buf, cbDest, TEXT("frequency = %f"), (float)result);
OutputDebugString(buf);
I also needed to remember to swap the debugger to handle mixed code:
Here is what I use most of the time (notice the "L"):
#include <Windows.h>
OutputDebugString(L"Sarah Connor ?\n");
This may be a vaugue question but im getting this error
Simulator.cpp: In member function `void Simulator::generatePassengers()':
Simulator.cpp:60: error: `itoa' undeclared (first use this function)
This is the code im having an issue with
itoa(i,buf,10);
What can i use to fix this issue because on one compiler i get this issue on another i dont. So im stumped here has to work on both.
char buf[3];
if(i<10)
{
key1.append("0");
key2.append("0");
key3.append("0");
}
itoa(i,buf,10);
key1.append(buf);
key2.append(buf);
key3.append(buf);
Your platform doesn't have itoa. It's not specified by any standard. You can use any replacement you like, including printf or just writing your own.
Does this work?
#include <string>
std::string buf = std::to_string(i);
key1.append(buf);
key2.append(buf);
key3.append(buf);
I have a problem that appears and disappears for mysterious reasons. A while back when I started the project I've found a fairly handy function that allows debug window output in VS2010. It worked great for a while.
Now it displays errors inconsistently. That means that sometimes the code will compile, sometimes it does not, and I can't find out why with code below causing the error. It seems almost random. Press compile, error, press compile again without changing anything sometimes error sometimes fine.
This is what it looks like with the error:
http://clip2net.com/clip/m0/1332710747-clip-29kb.png
and without:
http://clip2net.com/clip/m0/1332737362-clip-40kb.png
The culprit is OutputDebugString(buf);
Error doesn't occur with that line commented out.
I am looking to solve this problem, i simply need a way to output text into debug window (output), and am looking for a simple, stable solution. Or perhaps there is a way to make this function work. I am kind of stuck.
I would appreciate it if you could share how you do it.
The code is:
#pragma once
#ifndef _XDEBUG_H_
#define _XDEBUG_H_
#include <stdio.h>
#include <stdarg.h>
#include <ctype.h>
class XDebug
{
public:
static void __cdecl odprintf(const wchar_t *format, ...){
wchar_t buf[4096], *p = buf;
va_list args;
int n;
va_start(args, format);
n = _vsnwprintf(p, sizeof buf - 3, format, args); // buf-3 is room for CR/LF/NUL
va_end(args);
p += (n < 0) ? sizeof buf - 3 : n;
while ( p > buf && isspace(p[-1]) )
*--p = '\0';
*p++ = '\r';
*p++ = '\n';
*p = '\0';
OutputDebugString(buf);
}
};
#endif
OutputDebugString is defined in Windows.h. You need to include that header to be able to use that function.
It looks like you haven't done:
#include <windows.h>
The OutputDebugString function is part of the Windows API.
When I try to compile my code with the function GetLongPathName(), the compiler tells me that the function is undeclared.
I have already read the MSDN documentation located # http://msdn.microsoft.com/en-us/library/aa364980%28VS.85%29.aspx. But, even though I included those header files, I am still getting the undeclared function error. Which header file(s) am I supposed to include when using the function?
#include <Windows.h>
#include <WinBase.h>
#define DLLEXPORT extern "C" __declspec(dllexport)
DLLEXPORT char* file_get_long(char* path_original)
{
long length = 0;
TCHAR* buffer = NULL;
if(!path_original)
{
return "-10";
}
length = GetLongPathName(path_original, NULL, 0);
if(length == 0)
{
return "-10";
}
buffer = new TCHAR[length];
length = GetLongPathName(path_original, buffer, length);
if(length == 0)
{
return "-10";
}
return buffer;
}
And, if it makes a difference, I am currently compiling using Dev-C++ on a Windows Vista 64-bit.
Dev-C++'s support of the Windows API is not complete. Actually, it's not even close. It is entirely likely that the GetLongPathName function is not declared in winbase.h that is shipped with that compiler (Actually an old version of MinGW).
You can use the free compiler which ships with the Windows SDK to work around the problem. It is the same compiler that ships with Visual Studio, though it is commandline only.
You can also use Visual C++ Express Edition, which is free and provides features similar to DevCPP.
My problem is that when I want to make a downloaded library I get some weird compile errors from GCC and the code that the compiler demands to correct seems just to be right.
The errors are all like this:
Catalogue.h:96: error: there are no
arguments to ‘strlen’ that depend on a
template parameter, so a declaration
of ‘strlen’ must be available
Here is the code around line 96:
GaCatalogueEntry(const char* name, T* data)
{
if( name )
{
_nameLength = (int)strlen( name ); // LINE 96
// copy name
_name = new char[ _nameLength + 1 ];
strcpy( _name, name ); // LINE 100: similar error
_data = data;
return;
}
_name = NULL;
_nameLength = 0;
_data = NULL;
}
What can I do to fix these compile errors?
You probably just need to include the header that contains the strcpy and strlen library functions.
#include <string.h>
or (preferably for C++)
#include <cstring>
In C++ the strlen() function is part of the string library, and it almost looks like the header file was not included.
Is it included anywhere?
include <string.h>
If not, try adding it and see if that fixes the problem.
The code is buggy. You are probably missing an #include <string.h>.
If you don't want to change the code, add -fpermissive to the compiler options. (See the GCC documentation.)
a declaration of ‘strlen’ must be available
Include string.h or <cstring> (C++) for the declaration of strlen().