|
_Flaviu wrote: the project is not multi-byte
Is it "not multi-byte" or "multi-byte"?
|
|
|
|
|
Sorry, is multi-byte .. .anyway, if the project is unicode or is multi-byte, is the same thing.
|
|
|
|
|
Try these tests:
CStringA ansiText("TestA");
LPCSTR ansi = (LPCSTR)ansiText;
TRACE("%s\n", ansi);
CString someText("Test");
ansi = (LPCSTR)someText;
TRACE("%s\n", ansi);
What is the result?
|
|
|
|
|
with the first try:
CStringA ansiText("TestA");
LPCSTR ansi = (LPCSTR)ansiText;
TRACE("%s\n", ansi);
the result is TestA
for the second try, I got an error:
1>d:\tempx\test\testdoc.cpp(53): error C2440: 'type cast' : cannot convert from 'CString' to 'LPCSTR'
at
CString someText("Test");
ansi = (LPCSTR)someText; TRACE("%s\n", ansi);
The project is unicode.
|
|
|
|
|
_Flaviu wrote: for the second try, I got an error:
1>d:\tempx\test\testdoc.cpp(53): error C2440: 'type cast' : cannot convert from 'CString' to 'LPCSTR'
at
CString someText("Test");
ansi = (LPCSTR)someText; TRACE("%s\n", ansi);
The project is unicode. |
Of course you get the error since the CString contains the wide char text!
Either change the build to be MBCS/ANSI or convert CString to wchar_t* (or use _T() macro).
|
|
|
|
|
"build to be MBCS/ANSI" you meant multi-byte project ?
|
|
|
|
|
Go to Project -> Properties -> General and make sure Character set is set to "Use Multi-Byte Charachter Set".
|
|
|
|
|
_Flaviu wrote:
TRACE(">>>>>>>>>>>>>>>>>>>%s\n", chTemp); What if you try:
TRACE(">>>>>>>>>>>>>>>>>>>%S\n", chTemp);
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Is obviously something in project settings, because I have tried this code in a new test project and goes fine as multi-byte project ... as unicode, the same result.
|
|
|
|
|
You cannot cast a CStingW (which is what the class will be in a Unicode environment) to an LPCSTR (which is const char* ). You need to show again the actual failing code, and check the project settings.
|
|
|
|
|
In an array of strings I would like to get the string containing the most characters.
There are, of course, many ways to solve this, but coming from C# I find LINQ being an elegant tool.
I found out that the cpplinq NuGet package give me the same LINQ library as in C#.
For my problem - get the longest string in a string array - I can use the LINQ's aggregate function.
In C#, I have looked it up, and it can be solved like this:
using System;
using System.Linq;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var words = new string[] { "a", "bb", "ccc", "dddd" };
var longest = words.Aggregate("", (max, cur) => max.Length > cur.Length ? max: cur);
Console.WriteLine(longest);
}
}
} I cannot understand how to do this in C++ with the cpplinq package. Basically just porting the C#'s call to a C++'s call of the aggregate function.
#include "pch.h"
#include "cpplinq.hpp"
#include <iostream>
#include <string>
int main()
{
std::string words[4] = { "a", "bb", "ccc", "dddd" };
auto longest = "";
std::cout << longest << std::endl;
} I have googled for an example in c++ how to use the Aggregate function in cpplinq package, but with no result
Any suggestions?
|
|
|
|
|
I've just tried it out here and it seems easy enough:
using namespace cpplinq;
std::string words[4] = { "a", "bb", "ccc", "dddd" };
auto longest = from_array(words)
>> aggregate(std::string(), [](const std::string &m, const std::string &c) {
return m.length() > c.length() ? m : c; });
(I renamed the variables to "m" and "c" because I got an error message about max being a function.)
|
|
|
|
|
Thank you!
Man! I would have never guessed the porting would look like that 
|
|
|
|
|
I have an header file, where I have a function:
#ifdef __cplusplus
extern "C" {
#endif
....
struct_t* function_new(const struct_type_t* test);
...
#ifdef __cplusplus
}
#endif
for this function I get 2 links errors:
error LNK2019: unresolved external symbol _function_new referenced in function ....
now, in cpp file, of course that I have the body of this function, but is not recognized at all ... if this body function is written in cpp, or if I would delete the body of this function, the error is exactly the same ... strange, no ?
What could be the problem here, I am struggle to solve this error for days ...
|
|
|
|
|
For some reason the implementation file is not being built into your project. Once again we need more details in order to help you.
I would suggest that you move to using the Visual Studio IDE to build your projects as it is easier to see why problems like this occur, and you also get a standard structure for your projects.
|
|
|
|
|
The name function_new in your header file and the name _function_new reported by the linker do not match. Maybe the start of a track to explore here?
while (!(success = Try()));
|
|
|
|
|
The compiler adds the underscore at the beginning of function names in object files.
|
|
|
|
|
Some things that might have gone wrong:
1. You did not link the object file containing the implementation to your program.
Check your build options to make sure it's there.
2. The declaration in your header does not match your implementation.
Make sure there is no typo, the argument type and number matches, and ...
3. ... that it's compiled as C code. You mentioned a cpp file: if your compiler deducts how to compile a file by it's suffix, it may have compiled a C++ instead of a C function! In that case the linker won't find it. You could check your compiler's documentation for an option to force compilation as C. Alternately, just to find out you're on the right track, try removing 'extern "C"' in your header to find out whether that's actually the cause.
P.S.: Here's a good site pointing out all the details you need to watch out for when mixing C++ and C:
How to mix C and C++[^]
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
"2. The declaration in your header does not match your implementation.
Make sure there is no typo, the argument type and number matches, and ..."
That was the problem. Thank you all of you !
|
|
|
|
|
What could be the cause where a pdb files is not generated ? It is a warning generated by a VS2008 compiler used in command line mode ... Why others .pdb files is generated and this one (utils.pdb) are not ?
warning LNK4099: PDB 'utils.pdb' was not found with '.\..\..\retail\some.lib' or at 'D:\.....\retail\utils.pdb'; linking object as if no debug info
How can I understand this message ?
modified 27-Aug-19 4:24am.
|
|
|
|
|
You should look at your project settings, and build logs. You appear to have a somewhat complex directory setup and it is possible the PDB file could not be written for some reason.
|
|
|
|
|
The warning from above I had taken from build logs, but I don't know where I could find the project settings, I use compiler in command line mode ... I don't have any GUI for this environment ... hmm ..
|
|
|
|
|
Sorry, but we have no idea what your setup looks like so it is impossible to guess how things have been set up. If you use the Visual Studio IDE then project settings will be in a file called <project_name>.vcxproj or some similar extension. You need to provide some more details of how your project is setup and what commands you run to build it.
See also: Linker Tools Warning LNK4099 | Microsoft Docs[^].
|
|
|
|
|
#include <stdio.h>
#include <stdlib.h>
int main() {
int a,b,i,c;
b=0;
scanf("%d",&a);
i=a;
while(i<0){
i=a;
b=0;
b=b*10+i%2;
printf("%d",b);
c=i%2;
switch(c){
case '0':
i=i/2;
case '1':
i=(i-1)/2;
}
}
printf("%d",b);
return 0;
}
|
|
|
|
|
You have already posted this homework in QA:
Convert decimal number to binary[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|