小编S.F*_*rrd的帖子

是否可以在extern"C"函数中使用模板参数

我的要求是使用extern"c"函数从集合中获取项目.方法如下

template<class _Ty,
class _Alloc = allocator<_Ty> >
extern "C" __declspec(dllexport) _Ty* __cdecl GetItem(std::vector<_Alloc>* itr, int index)
{
    if (itr->size() < index)
        return NULL;

    return &itr->at(index);
}
Run Code Online (Sandbox Code Playgroud)

编译时遇到如下错误

error C2988: unrecognizable template declaration/definition
Run Code Online (Sandbox Code Playgroud)

使用此extern方法是使用pinvokefrom 获取对象数据c#

c++

8
推荐指数
1
解决办法
324
查看次数

堆分配对象中的堆栈对象在c ++中的位置分配在哪里?

有两个班荫Class AClass B.

class A
{
  int width;
  int height;  

};

class B
{
  A obj;

};
Run Code Online (Sandbox Code Playgroud)

我正在尝试创建一个如下所示的指针

B* myObj = new B();
Run Code Online (Sandbox Code Playgroud)

在这里,myObj在堆中创建.在哪里obj,widthheight创建?

c++

2
推荐指数
1
解决办法
71
查看次数

错误 C2760:语法错误:意外的标记“标识符”,预期为“;” 在地图迭代器

我创建了一个继承std::map并尝试使用方法在特定索引处获取值的类。

#define MYAPI_EXPORTS
#ifdef MYAPI_EXPORTS  
#define MY_API __declspec(dllexport)
#else  
#define MY_API __declspec(dllimport)   
#endif

    template<class _Value>
    class MY_API MyDictionary : public std::map<std::string, _Value>
    {
        _Value GetItem(int index)
        {
            std::map<std::string, _Value>::iterator itr = this->begin(); //compile error at this line

            int c = 0;
            while (c < index)
            {
                itr++;
                c++;
            }
            return itr->second;
        }
    };
Run Code Online (Sandbox Code Playgroud)

'std::map::iterator itr' 这一行在编译时显示错误。

错误是

error C2760: syntax error: unexpected token 'identifier', expected ';'
error C7510: 'iterator': use of dependent type name must be prefixed with …
Run Code Online (Sandbox Code Playgroud)

c++

2
推荐指数
2
解决办法
1万
查看次数

即使对象超出范围,Dotnet也不会调用它的终结器.那么如何释放非托管资源呢?

我尝试使用以下代码

[DllImport("Core.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr CreateNode();

[DllImport("Core.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern void ReleaseNode(IntPtr handle);

 class Node
{
    IntPtr nativePtr;
    public int id;
    public Node(int i)
    {
        nativePtr = CreateNode();
        id = i;
        Debug.WriteLine("Constructed " + id);
    }

    ~Node()
    {
        ReleaseNode(nativePtr);
        Debug.WriteLine("Destructed " + id);
    }
}

    class Program
    {
        static void Main(string[] args)
        {

            for (int i = 0; i < 10; i++)
            {
                Node n = new …
Run Code Online (Sandbox Code Playgroud)

c# unmanaged unmanagedresources

0
推荐指数
1
解决办法
88
查看次数

在C ++中使用三元运算符总是返回错误条件

我正在使用带有三元运算符的语句,该语句始终返回其他值。

BSTR pVal = L"Yes";

bool val = pVal == L"Yes" ? true : false;
Run Code Online (Sandbox Code Playgroud)

该语句返回

 val = false;
Run Code Online (Sandbox Code Playgroud)

我希望它在这里返回true。我做错了吗?

c++

-2
推荐指数
1
解决办法
62
查看次数

C++中函数中Const使用的区别

我有以下方法使用多个const关键字.他们为什么用?

const int* MyClass::getvalue(const int input) const
Run Code Online (Sandbox Code Playgroud)

如果从方法返回指针,有什么方法可以限制用户更改指针值和指针本身?

c++

-3
推荐指数
1
解决办法
77
查看次数

标签 统计

c++ ×5

c# ×1

unmanaged ×1

unmanagedresources ×1