使用 g++ 编译主模块时出现奇怪的错误

发布于 2025-01-01 17:35:45 字数 1387 浏览 1 评论 0原文

我正在尝试使用“g++ main.cpp -c”编译以下代码,但它给了我这个奇怪的错误..有什么想法吗?

main.cpp: In function ‘int main()’:
main.cpp:9:17: error: invalid conversion from ‘Graph*’ to ‘int’
main.cpp:9:17: error:   initializing argument 1 of ‘Graph::Graph(int)’
main.cpp:10:16: warning: deprecated conversion from string constant to ‘char*’

这是我正在尝试编译的主模块,下面是我在 graph.hpp 中的图形类

#include <iostream>
#include "graph.hpp"

using namespace std;

int main()
{
  Graph g;
  g = new Graph();
  char* path = "graph.csv";
  g.createGraph(path);
  return 0;
}

,这是我的图形类

    /*
 * graph.hpp
 *
 *  Created on: Jan 28, 2012
 *      Author: ajinkya
 */

#ifndef _GRAPH_HPP_
#define _GRAPH_HPP_

#include "street.hpp"
#include "isection.hpp"
#include <vector>

class Graph
{
 public:
  Graph(const int vertexCount = 0);
  //void addIsection(Isection is);
  //void removeIsection(int iSectionId);
  Isection* findIsection(int);
  void addStreet(int iSection1, int iSection2, int weight);
  void createGraph(const char *path); //uses adj matrix stored in a flat file
  //void removeStreet(int streetID);
  void printGraph();
  ~Graph();
 private:
  //Isection *parkingLot;
  //Isection *freeWay;
  int** adjMatrix;
  std::vector <Street*> edgeList;
  std::vector <Isection*> nodeList;
  int vertexCount;
};

    #endif

I am trying to compile the below code using "g++ main.cpp -c" but it gives me this strange error .. any ideas?

main.cpp: In function ‘int main()’:
main.cpp:9:17: error: invalid conversion from ‘Graph*’ to ‘int’
main.cpp:9:17: error:   initializing argument 1 of ‘Graph::Graph(int)’
main.cpp:10:16: warning: deprecated conversion from string constant to ‘char*’

This is my main module which i am trying to compile and below that is the graph class i have in graph.hpp

#include <iostream>
#include "graph.hpp"

using namespace std;

int main()
{
  Graph g;
  g = new Graph();
  char* path = "graph.csv";
  g.createGraph(path);
  return 0;
}

AND this is my Graph class

    /*
 * graph.hpp
 *
 *  Created on: Jan 28, 2012
 *      Author: ajinkya
 */

#ifndef _GRAPH_HPP_
#define _GRAPH_HPP_

#include "street.hpp"
#include "isection.hpp"
#include <vector>

class Graph
{
 public:
  Graph(const int vertexCount = 0);
  //void addIsection(Isection is);
  //void removeIsection(int iSectionId);
  Isection* findIsection(int);
  void addStreet(int iSection1, int iSection2, int weight);
  void createGraph(const char *path); //uses adj matrix stored in a flat file
  //void removeStreet(int streetID);
  void printGraph();
  ~Graph();
 private:
  //Isection *parkingLot;
  //Isection *freeWay;
  int** adjMatrix;
  std::vector <Street*> edgeList;
  std::vector <Isection*> nodeList;
  int vertexCount;
};

    #endif

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

残疾 2025-01-08 17:35:45

这是 C++,而不是 Java 或 C#。 new 在这里的工作方式不同。

new 表达式返回指针。您不能将指向 Graph 的指针(即 Graph*)分配给 Graph 类型的变量:

Graph g;
g = new Graph(); // Graph = Graph* ? nope

似乎编译器试图将“有用”并尝试使用构造函数需要一个 int 参数来生成 Graph 类型的值,但它无法转换 Graph* 转换为 int

当您编写Graph g;时,您已经有了一个Graph对象。您不需要使用 new 创建一个。事实上,您可能甚至不想这样做,因为这会导致内存泄漏

然后是这一行:

char* path = "graph.csv";

"graph.csv" 的类型为 char const[10],因此您不应将其分配给 char*。过去你可以,但事实证明这是一个坏主意。该功能被标记为已弃用,现在在 C++ 中已完全删除。您可以不这样做,而是:

  • 用它创建一个数组:char path[] = "graph.csv";;
  • 使用正确的类型创建指向它的指针:char const* path = "graph.csv";(这是有效的,因为数组类型衰减为指针);

This is C++, not Java or C#. new doesn't work the same way here.

new expressions return pointers. You cannot assign a pointer to a Graph (i.e. a Graph*) to a variable of type Graph:

Graph g;
g = new Graph(); // Graph = Graph* ? nope

Seems like the compiler is trying to be "helpful" and trying to use your constructor take takes an int argument to make a value of type Graph, but it can't convert a Graph* to an int.

When you write Graph g; you already have a Graph object. You don't need to create one with new. In fact, you probably don't even want to do that, as it will lead to memory leaks.

Then there's this line:

char* path = "graph.csv";

"graph.csv" has type char const[10] so you should not assign it to a char*. In the past you could, but that turned out to be a bad idea. That feature was marked as deprecated, and now it was completely removed in C++. Instead of doing that you can:

  • Make an array out of it: char path[] = "graph.csv";;
  • Make a pointer to it with the proper type: char const* path = "graph.csv"; (this works because array types decay to pointers);
薄暮涼年 2025-01-08 17:35:45

这可能应该是 g = Graph(); 并忘记 g = new Graph;。原因是 new 返回一个指向所创建对象的指针(例如Graph*),而不是对象值。

更好的是,只需执行 Graph g; 即可,而无需为 g 分配任何内容。这将通过调用 Graph 的无参数构造函数自动为您创建一个 Graph

That should probably be g = Graph(); and forget the g = new Graph;. The reason is that new returns a pointer to the created object (e.g. a Graph*), not an object value.

Better still, just do Graph g; and forget about assigning anything to g. This will automatically create a Graph for you by calling Graph's no-arg constructor.

抹茶夏天i‖ 2025-01-08 17:35:45

Graph* g;

它必须是一个指针。

Graph* g;

It has to be a pointer.

德意的啸 2025-01-08 17:35:45

首先,new Graph() 返回一个 Graph*。在 C++ 中,可以隐式调用具有一个参数的构造函数,因此当前您的代码的真正含义是:

g = Graph(new Graph());

您想要的是

g = Graph();

First of all new Graph() returns a Graph*. In c++, a constructor with one argument can be implicitly called, so currently your code is really meaning:

g = Graph(new Graph());

What you want is

g = Graph();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文