小编Ted*_*gmo的帖子

mac中的分段错误,但不是其他在线编译器

此代码在ideone和其他编译器上运行,但在我的mac或一些垃圾值中给出了分段错误.请帮忙.这是遍历顺序的标准,应该只打印1到7.

https://ideone.com/l5tkks

#include <bits/stdc++.h>
using namespace std;

struct node{
    int data;
    struct node* left;
    struct node* right;
};

typedef struct node* Node;

Node insert(Node root, int num){
    if(root==NULL){
        Node newNode=(Node)malloc(sizeof(Node));
        newNode->data=num;
        newNode->left=NULL;
        newNode->right=NULL;
        return newNode;
    }

    if(root->data>num)
        root->left=insert(root->left,num);
    else
        root->right=insert(root->right,num);
    return root;
}

void printinorder(Node root){
    if(root==NULL)
        return;

    printinorder(root->left);
    cout<<root->data<<endl;
    printinorder(root->right);
}

int main(){
    Node tree=NULL;
    tree=insert(tree,1);
    tree=insert(tree,2);
    tree=insert(tree,3);
    tree=insert(tree,4);
    tree=insert(tree,5);
    tree=insert(tree,6);
    tree=insert(tree,7);
    printinorder(tree);
}
Run Code Online (Sandbox Code Playgroud)

c++ macos

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

'this' 参数的类型为 const 但函数未标记为 const C++ 重载运算符

我该如何解决?

我收到错误: 'this' argument has type const but function is not marked const c++ overload operator

template <class T>
class Rational {
private:
    T n = 0;
    T d = 1;
public:
    Rational() = default;
    T numerator() {
        return n;
    }
    T denominator() {
        return d;
    }
};

template <class T>
inline bool const operator ==(const Rational <T> & lhs, const Rational <T>& rhs)  {

    return lhs.numerator() * rhs.denominator() == lhs.denominator() * rhs.numerator();

}
Run Code Online (Sandbox Code Playgroud)

c++

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

C++ 检测类型是否为字符串对象

这有点奇怪。我正在尝试编写一个代码来检测类型是否为字符串对象。char * 或其他对象类型应该导致 false 而不是 true。以下代码给了我:

错误:模板参数在部分特化中不可推导:

我不明白该消息是什么意思。即使在网上搜索,我也不知道如何解决这个问题:

#include <iostream>
#include <type_traits>
#include <string>

template <typename T>
struct is_string : std::false_type { };

template <typename T>
struct is_string<std::string> : std::true_type<T> { };

class Temp
{
  int a;
};

int main()
{
    // Expect: false
    std::cout << is_string<int>::value << std::endl;
    std::cout << is_string<char *>::value << std::endl;
    std::cout << is_string<Temp>::value << std::endl;

    // Expect: true
    std::cout << is_string<std::string>::value << std::endl;
    std::cout << is_string<const std::string>::value << std::endl;
    std::cout << is_string<std::string&>::value << std::endl;
} …
Run Code Online (Sandbox Code Playgroud)

c++ templates type-traits c++17

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

无法理解范围解析运算符的使用

::mediapipe::Status RunMPPGraph() {
  std::string calculator_graph_config_contents;
  MP_RETURN_IF_ERROR(mediapipe::file::GetContents(
      FLAGS_calculator_graph_config_file, &calculator_graph_config_contents));
  LOG(INFO) << "Get calculator graph config contents: "
            << calculator_graph_config_contents;
  mediapipe::CalculatorGraphConfig config =
      mediapipe::ParseTextProtoOrDie<mediapipe::CalculatorGraphConfig>(
          calculator_graph_config_contents);
Run Code Online (Sandbox Code Playgroud)

这是 Google 的Mediapipe提供的较大代码的一小部分,它使用范围解析运算符来定义RunMPPGraph(). 我对这个定义一无所知。有人可以告诉我这是怎么回事吗?

这看起来像一个函数,我很确定它是:::mediapipe::Status RunMPPGraph()... 但是定义函数的基本方法是 ---> ReturnType FunctionName(parameters),在这个程序中RunMPPGraph是名称,所以这意味着::mediapipe::Status是返回类型。在主函数中,RunMPPGraph() 是用这个语句 ---> 调用的,::mediapipe::Status run_status = RunMPPGraph();所以这意味着::mediapipe::Status是某种形式的用户定义的数据类型。所以我想知道我们是否可以分解::mediapipe::Status成更小的部分?

c++ function mediapipe

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

对 C++ 显式强制转换的更改

我还没有接触过 C++20。是否有任何改变C ++明确的类型转换操作符(static_castdynamic_castconst_castreinterpret_cast在C ++ 20)?也就是说,任何改进、弃用或新的演员阵容?例如,是否有任何改进可以reinterpret_cast减少出错的可能性?

c++ casting type-conversion c++20

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

类型“int”的值不能分配给类型“Node&lt;int&gt; *”C/C++(513) 的实体

我有一个链表,其中包含指向第一个和最后一个节点的指针以及指示列表中有多少个节点的大小。\n我有一个返回第一个节点的函数。

\n

我希望能够使用 更改第一个节点中的 m_data queue1.front() = 3;。然而,我得到

\n
invalid conversion from \xe2\x80\x98int\xe2\x80\x99 to \xe2\x80\x98Node<int>*\xe2\x80\x99\n
Run Code Online (Sandbox Code Playgroud)\n

编译时出错

\n
invalid conversion from \xe2\x80\x98int\xe2\x80\x99 to \xe2\x80\x98Node<int>*\xe2\x80\x99\n
Run Code Online (Sandbox Code Playgroud)\n
template <class T>\nclass Node {\npublic:\n    Node(const T& t);\n    ~Node() = default;            // Destructor\n    Node(const Node&) = default;  // Copy Constructor set to default\n    Node& operator=(const Node&) =\n        default;  // Assignment operator set to default\n    T& getData();\n    const T& getData() const;\n    Node* getNext();\n    void setNext(Node<T>* newNext);\n\nprivate:\n    T m_data;\n    Node* m_nextNode;\n};\n\ntemplate <class T>\nNode<T>::Node(const T& t) {\n …
Run Code Online (Sandbox Code Playgroud)

c++ templates class class-template c++11

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

如何使用基类的 std::hash 对类型进行哈希处理?

我有一个C<B>继承自 的类模板std::string,我想让它像普通的 一样可散列std::string。我编写了以下代码,并且可以编译。

我想知道这是否是正确的实现。由于从派生类到基类的转换可能会截断内存,我想知道它是否会花费太多?

#include <unordered_set>
#include <string>

template <bool B>
struct C : std::string
{
    C(std::string s) : std::string(s) {}
};

namespace std {

    template <bool B>
    struct hash<C<B>>
    {
        std::size_t operator()(const C<B> &k) const {
            return std::hash<std::string>()(static_cast<std::string>(k));
        }
    };

} // namespace std

int main() {
    std::unordered_set<C<false>> s;
    std::string c = C<false>("a");
    s.insert(c);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ templates template-specialization stdhash

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

编程接收到的信号SIGSEGV,在代码块中调试时出现分段错误

我是C ++的初学者,我正在尝试解决https://projecteuler.net/problem=9。我为此写了一个代码,它显示了错误-程序收到信号SIGSEGV,分段错误。在调试时在strcmp()(C:\ Windows \ syswow64 \ msvcrt.dll)中。

如果我直接运行该程序,则会出现一个对话框,显示“ Windows正在检查解决方案”。

我试过不使用字符串函数,而不是写pytha(a,b,c)==“ true”,我只是写了axa + bxb = c * c(我写了*而不是x,但是这里没有显示*在两个a之间,因此我将其替换为x),并且代码运行正常。但问题是为什么它不能与字符串函数一起使用?

我没有看到代码有什么问题。

我发现了很多类似的问题-1. https://www.codeproject.com/Questions/93770/what-is-this-means-Program-received-signal-SIGSEGV
根据这一点,我的程序是指它无权访问的内存位置。但是我看不到任何限制此代码访问某些内容的东西。

  1. 程序收到信号SIGSEGV,分段故障错误

3. 调试---编程接收到的信号SIGSEGV,分段错误

  1. 程序收到信号SIGSEGV,分段错误

  2. “程序收到信号SIGSEGV,分段故障。”

  3. 程序收到信号SIGSEGV,分段故障

  4. 程序收到信号SIGSEGV,分段故障,链表程序

他们中没有一个回答我的查询,因为我无法将其中提到的代码与我的代码相关联。编号为5的链接提到该错误可能是由于涉及大量计算。即使我对我的代码也有疑问,但是当我不使用“ pytha”函数时,它可以很好地工作。另外,我看不到涉及与内存访问相关的错误的大量步骤。

同样,即使是涉及大量步骤的原因,程序也应在有足够时间的情况下进行编译。但事实并非如此。它清楚地显示了“ Windows正在寻找解决方案”的错误。

#include <cmath>
#include <iostream>
#include <string>

using namespace std;

string pytha(int a, int b, int c) {
    if(a * a + b * b == c * c) return "true";
}

int main() {
    for(int a = 1; a < 1000; a++) {
        for(int b = …
Run Code Online (Sandbox Code Playgroud)

c++

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

错误消息“分段错误(核心已转储)”

我正在尝试运行以下 C 程序:

/*
 * Illustrates system call fork
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int
main (
  int argc,
  char *argv[]
  )
{
  int tC, tF;
  pid_t pid;

  tC = atoi (argv[1]);
  tF = atoi (argv[2]);

  fprintf (stdout, "Main  :                  ");
  fprintf (stdout, "PID=%d; My parent PID=%d\n",
    getpid(), getppid());

  pid = fork();
  if (pid == 0){
    // Child
    sleep (tC);

    fprintf(stdout, "Child : PIDreturned=%d    ", pid);
    fprintf (stdout, "PID=%d; My parent PID=%d\n",
      getpid(), getppid());
  } else {
    // …
Run Code Online (Sandbox Code Playgroud)

c posix

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

指向 const 的指针:为什么 ptr[1] 也是只读的?

我理解,指向const的指针是一个指针,通过该指针无法更改该指针所指向的变量的值。因此,在下面的示例中,我希望编译器会引发错误:

\n
int main()\n{\n    int a = 42;\n    const int *ptr = &a;\n    ptr[0]++;\n\n    return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

现在,为什么下面的示例也会出现错误?

\n
int main()\n{\n    int a = 42;\n    const int *ptr = &a;\n    ptr[1]++;\n\n    return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

第二个示例(使用ptr[1]++)抛出error: increment of read-only location \xe2\x80\x98*(ptr + 4)\xe2\x80\x99。因此,看起来不仅指针指向的变量的值不能改变,其他相关内存位置的值也不能改变。

\n

与此相关的具体行为是什么?此行为在哪里引用?例如,https://en.cppreference.com/w/c/language/pointer对我来说似乎并不清楚。

\n

c pointers

0
推荐指数
2
解决办法
153
查看次数