Visual Studio c++: how to generate function calls with its parameters - c++

I noticed that the auto-complete options like [Cltr + space] in Visual Studio IDE can only paste function names.
Here is an c++ example code:
class TestClass
{
public:
explicit TestClass();
virtual ~TestClass();
void callThisMethod(int a, char ch, string s);
}
// TestClass.cpp file
void TestClass:callThisMethod(int a, char ch, string s){.....}
// main.cpp file:
TestClass tc;
tc.callThisMethod(int a, char ch, string s); // here
So is there any way to bring function calls with all necessary parameters.

The Visual Studio correctly designed for what it does:
The intellisense will make you on the right track by keeping showing the function signature, so that you would know which parameters are actually required. For an instance:
From the example, it can be clearly seen here that as longer as you type and provide the arguments, the popup will help you to go through the function signature.
Thus, it doesn't seems the feature you're asking is currently available, neither it's required due to the intellisense which actually helps you a lot in this situation.

Related

Why can't we pass a reference from native C++ to C++/CLI?

I use a native C++ code base from C#, with a C++/CLI wrapper built around it (with Visual Studio 2013). There are two projects:
NativeCodeBase: simple C++ project set to be built into a static lib.
ManagedWrapper: C++/CLI project referencing NativeCodeBase.
I have the following native "interface" in NativeCodeBase:
class ITest {
virtual void Foo(const std::string& str, MyEnum me) = 0;
}
For which I have a native implementation in the ManagedWrapper project.
In the header:
class TestManaged : public ITest {
virtual void Foo(const std::string& str, MyEnum me) override;
}
In the cpp:
void TestManaged::Foo(const std::string& str, MyEnum me) {
int length = str.length();
}
The MyEnum enum is used both in native and managed code, so in its implementation I use a conditionally compiled C++/CLI extension, to make it usable from C#:
#ifdef _MANAGED
public
#endif
enum class MyEnum : unsigned char
{
Baz = 0,
Qux = 1
};
In my native code I have a reference to ITest and call its Foo function with a local std::string variable. When Foo is called, I can see in the debugger that the string passed as an argument is a valid string object.
The call is similar to this:
void Bar(ITest& test) {
std::string str = "test";
test.Foo(str, MyEnum::Baz);
}
However, if I put a breakpoint at the beginning of TestManaged::Foo, the debugger says that str has <undefined value>, and the length() call crashes with undefined reference error in the <xstring> header in the following function:
size_type length() const _NOEXCEPT
{ // return length of sequence
return (this->_Mysize);
}
The debugger displays <undefined value> for the this pointer as well.
What can be the reason for this? References somehow get corrupted when passed between the two libraries?
(Additional info: I used not to build the NativeCodeBase project as a separate lib, but linked all the source files from it into the CLI project, and the same code base worked without any problem. It started failing since I configured it to be built into a separate lib and added a reference in the CLI project to the native one.)
The problem wasn't with the reference itself. The problem was with the second enum parameter. The implementation of the enum class looked like this:
#ifdef _MANAGED
public
#endif
enum class MyEnum : unsigned char
{
Baz = 0,
Qux = 1
};
The #ifdef directive was put there in order to create a native enum when built for native C++, but create a CLI enum when built for C++/CLI.
This worked well when all the source files were linked to the CLI project and every piece of source was built again for the CLI project. However, this approach does not work any more when I want to use the native lib from the CLI side.
I guess the problem was that the same header was built differently in the two libraries, so the caller and the calle saw a different binary interface of the object, thus the arguments got garbled when passed. Is this correct?
I got rid of the conditionally compiled public keyword and it started working properly again.

Optional Parameters in VS2010 templitized class function

I have an odd issue in MSVS 2010. I have a class with a function that is templitized and contains an parameter with a default value.
In my header file:
typedef unsinged int data32
class myClass
{
private:
...
public:
...
template <typename T>
T* myF(data32);
}
...
template<typename T>
T* myClass::myF(data32 size = 1)
{
...
}
Ok, now in my main i have something like this:
int main()
{
myClass A;
data32* myInt = A.myF<data32>(100); // no complaints from pre-compiler
data32* myInt2 = A.myF<data32>(); // pre-compiler complains "Error: no instance of the function template "myClass::myF" matches the argument list"
}
I understand why it is unhappy as i do not have a function prototype defined for 'myF()' in the class, but shouldn't it know better? I thought the point of defaults were to make the parameters optional in the call. The code DOES compile and run just fine even thought the pre-compiler is unhappy and flags this as a problem.
Any thoughts??
Thanks!
There are bugs (false alarms) in the intellisense analyzer in VS 2010. And this seems like one of them. The analyzer used for intellisense is different from the actual parser used in compiler.

How to handle with warning in C++ inline + template context?

I'm facing an interesting problem: I had an MFC application project in Visual C++ 6.0. Since there are many changes either in MFC or in C++ standard, I wanted to port my application to Visual Studio 2010. It was fine, but I am facing a warning now, which I can't handle.
The header file has the following class definition:
template <class T>
class foo : public CObject
{
// ...
// other stuff
// ...
private:
CTypedPtrMap<CMapWordToPtr, const long, T*> oElementMap;
void some_stuff();
}
In the source file there is:
template <class T>
void foo::some_stuff()
{
// ...
// other stuff
// ...
int nIndex = 0;
// ...
// other stuff
// ...
oElementMap.RemoveKey(nIndex);
}
When I try to compile this, I get the following warnings:
Warning 1 warning C4244: 'argument' : conversion from 'const long' to
'WORD', possible loss of data c:\programme\microsoft visual studio
10.0\vc\atlmfc\include\afxtempl.h 2066
It comes definetly from the above mentioned "RemoveKey" line: if I just simply comment out that line, I won't get this warning.
I know, the main problem is, that CTypedPtrMap object uses const long as key type, but CMapWordToPtr would have WORD (unsigned short) instead of it. But the fact is: I need const long as key type, since I am processing regulary about 1 million data entries in this map, so with unsigned short the class would not be capable to do it's job furthermore.
I tried to nest either the "RemoveKey" line or the include of stdafx.h into the following expressions, but neither worked:
#pragma warning (disable: 4244)
// expression
#pragma warning (default: 4244)
Please share me any ideas about this issue, how could I resolve this warning WITHOUT changing the container's oElementMap definition and behaviour, and WITHOUT supress/disable this warning globally in the project settings as well as WITHOUT changing the afxtempl.h file supplied by VS2010.
Thanks for help:
Andrew
I've replaced it's definition to: CMap<long, long&, T*, T*&> oElementMap;. I was not sure it is the "long-counterpart" of the old map definition, therefore I did several test to compare them.
The solution was finally this.

Two or more data types in declaration of 'parameters' using mingw cross compiler under linux

I am trying to cross compile some code for windows using MinGw. The code is fairly simple:
Header:
class DragLabel : public QLabel
{
Q_OBJECT
public:
DragLabel();
void fn(QString path, int id, bool small);
};
cpp:
#include "draglabel.h"
DragLabel::DragLabel()
{
/* Snip ... */
};
void DragLabel::fn(QString path, int id, bool small)
{
(void)d;
};
The example function fails to compile givin me:
error: two or more data types in declaration of 'parameter'
for the declaration of fn(QString...).
[EDIT:] Sorry i forgot to mention that this error happens only if there is the bool variable declared, so the function without:
void fn(QString path, int id);
Workes just fine.
It compiles fine using qmake and make under debian linux.
Does anyone know what might happens here?
Thanks
It seems that small is some extension keyword of MinGW (I couldn't find it in standard). According to
When i change everything to int it works
small is some qualifier like long or signed, that extends int declaration.
Try to change variable name from small to anything else.

Visual Studio 2012 "Smart" Indentation Customization

I am using Visual Studio 2012 and have Smart indentation turned on1 for C++ files.2 I would like to customize Smart indentation's behavior so that it formats the code I enter so that it complies with my company's coding style.
How can I customize all the minute aspects of how Smart indentation behaves?
For example, when I enter this code, Smart indentation formats it exactly like this:
#include <cstdlib>
#include <string>
using namespace std;
struct Foo
{
const string mA;
const int mB;
const string mC;
Foo(const string& a,
const int b,
const string& c)
:
mA(a),
mB(b),
mC(c)
{
}
};
int main()
{
}
Most of this is what I want, except for the colon introducing the initializer list, the first item in the initializer list, and the indentation level of the constructor's body. I want these formatted like this, and I want Visual Studio to do it for me automatically:
Foo(const string& a,
const int b,
const string& c)
:
mA(a),
mB(b),
mC(c)
{
}
How can I customize Smart indentation's behavior? I'd prefer to not use any external tools like Visual Assist X.
1: Via Tools > Options > Text Editor > C/C++ > Tabs > Indenting
2: I also have tabstops set to 4, with spaces inserted.
Look into the MS Visual Studio SDK, found here:
http://msdn.microsoft.com/en-us/library/bb139565.aspx
In particular you want to override HandleSmartIndent in the VewFilter class:
http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.package.viewfilter.handlesmartindent.aspx
This gets called whenever you press the Enter key in the editor. Unfortunately, it's not as easy as just changing some rules in a config dialog.
An ugly solution is this:
Foo(const string& a,
const int b,
const string& c)
: mA(a)
, mB(b)
, mC(c)
{
}
Which, for some abominable reason, is the only way I've ever seen to get VS to indent that mess properly.