【问题标题】:Why can't my C++ code see this C# class member?为什么我的 C++ 代码看不到这个 C# 类成员?
【发布时间】:2016-11-02 15:32:40
【问题描述】:

我在 C# 项目中有几个自定义类,然后我在 C++ 项目中引用它们。 C# 代码看起来像这样

namespace AllOptions
{
    public class AllOptions {
        public AlgorithmOptions algOptions{ get; set; }
        public DatabaseOptions dataOptions{ get; set; }
    }

    public class AlgorithmOptions {
        List<Algorithm> algorithms { get; set; }

        public void SetDefaults(){
            this.algorithms.Clear();
        }
    }
    public class Algorithm {
        public bool AllowSalt { get; set; }
    }
    public class DatabaseOptions {
        public List<string> databaseSrouces { get; set; }
    }
}

然后我尝试从 C++ 访问 AllOptions 的各个部分,但并非所有部分都通过。

//Is declared at the beginning
public: VerifyOptions::VerifyOptions Options;

//Then later on I try to access the database options and algorithm options
this->Options.databaseOptions->databaseSources = someStringList; //works fine

//This cannot find the algorithm list
this->Options.algorithmOptions->algorithms = someAlgorithmList; //does not work

算法说“Class AllOptions::AlgorithmOptions 没有成员算法”。为什么 C++ 代码看不到这个特定的 C# 成员?

编辑我的问题被标记为可能与此Default access modifier in C# 问题重复。但是,我相信这些是碰巧导致相同答案的不同问题。如果我的想法有误,请重新标记,我会更改它。

【问题讨论】:

  • 也许你在某个地方有错字。 C# 和 C++ 代码中的 databaseSroucesdatabaseSources 确实不匹配。
  • @NathanOliver,不幸的是,这只是我在这里打字不好,在代码中所有数据类型都匹配
  • 能否请您使用正确的标签。您同时拥有c#c++
  • @AndrewTruckle 这不是一个包含两者的问题吗?
  • 糟糕——我看错了第一句话——你使用了两种语言。我坏了!

标签: c# c++


【解决方案1】:

AlgorithmOptions.algorithms 是私人成员。只有包含类才能访​​问其私有成员。相比之下,DatabaseOptions.databaseSources 是公共成员,您可以从任何地方访问它。

【讨论】:

  • 我觉得很愚蠢,我忘记了标准保护级别是私有的,这确实是我的错误。感谢您的快速回复
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-10
  • 2018-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-07
相关资源
最近更新 更多