使用 g++ 编译主模块时出现奇怪的错误
我正在尝试使用“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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是 C++,而不是 Java 或 C#。
new
在这里的工作方式不同。new
表达式返回指针。您不能将指向Graph
的指针(即Graph*
)分配给Graph
类型的变量:似乎编译器试图将“有用”并尝试使用构造函数需要一个
int
参数来生成Graph
类型的值,但它无法转换Graph* 转换为
int
。当您编写
Graph g;
时,您已经有了一个Graph
对象。您不需要使用new
创建一个。事实上,您可能甚至不想这样做,因为这会导致内存泄漏。然后是这一行:
"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 aGraph
(i.e. aGraph*
) to a variable of typeGraph
: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 typeGraph
, but it can't convert aGraph*
to anint
.When you write
Graph g;
you already have aGraph
object. You don't need to create one withnew
. In fact, you probably don't even want to do that, as it will lead to memory leaks.Then there's this line:
"graph.csv"
has typechar const[10]
so you should not assign it to achar*
. 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:char path[] = "graph.csv";
;char const* path = "graph.csv";
(this works because array types decay to pointers);这可能应该是
g = Graph();
并忘记g = new Graph;
。原因是new
返回一个指向所创建对象的指针(例如Graph*
),而不是对象值。更好的是,只需执行
Graph g;
即可,而无需为g
分配任何内容。这将通过调用Graph
的无参数构造函数自动为您创建一个Graph
。That should probably be
g = Graph();
and forget theg = new Graph;
. The reason is thatnew
returns a pointer to the created object (e.g. aGraph*
), not an object value.Better still, just do
Graph g;
and forget about assigning anything tog
. This will automatically create aGraph
for you by callingGraph
's no-arg constructor.Graph* g;
它必须是一个指针。
Graph* g;
It has to be a pointer.
首先,
new Graph()
返回一个Graph*
。在 C++ 中,可以隐式调用具有一个参数的构造函数,因此当前您的代码的真正含义是:您想要的是
First of all
new Graph()
returns aGraph*
. In c++, a constructor with one argument can be implicitly called, so currently your code is really meaning:What you want is