Herzlich Willkommen, lieber Gast!
  Sie befinden sich hier:

  Forum » C / C++ (ANSI-Standard) » c++ kompelier problem

Forum | Hilfe | Team | Links | Impressum | > Suche < | Mitglieder | Registrieren | Einloggen
  Quicklinks: MSDN-Online || STL || clib Reference Grundlagen || Literatur || E-Books || Zubehör || > F.A.Q. < || Downloads   

Autor Thread - Seiten: > 1 < [ 2 ]
000
17.05.2009, 05:37 Uhr
xyz



Hallo,
leider bekomme ich beim kompelieren diese Fehlermeldung:

Code:
make all
Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o"main.o" "../main.cpp"
../main.cpp: In function 'int main()':
../main.cpp:22: error: conversion from 'Graph*' to non-scalar type 'Graph' requested
../main.cpp:32: error: conversion from 'GraphNode*' to non-scalar type 'GraphNode' requested
make: *** [main.o] Error 1



main.cpp

Code:
#include "graph.h"
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

struct s_node {
    int label;
    double x, y, z;
};

istream &operator>>(istream &in, s_node &n) {
    return in >> n.label >> n.x >> n.y >> n.z;
}

int main() {
    //make an empty graph, and an empty temporary graph node
    Graph graph = new Graph();
    s_node s_nodeTmp;

    ifstream in("template.txt");
    //construct the nodes
    while(in >> s_nodeTmp) {
        //v_nodes.push_back(s_nodeTmp);
        GraphNode tempNode = new GraphNode(s_nodeTmp.label,
                                           s_nodeTmp.x,
                                           s_nodeTmp.y,
                                           s_nodeTmp.z);
        graph.addNode(tempNode);

    }
    //construct the adjency
    for (int i = 0; i < graph.getNumNodes(); i++) {
        for (int j = 0; j < graph.getNumNodes(); j++) {
            if (graph.getNodeAt(i).getLabel() != graph.getNodeAt(j).getLabel()){
                graph.getNodeAt(i).addAdjacentNode(graph.getNodeAt(j).getLabel());
            }
        }
    }

    //print the graph
    graph.print();

    return 0;
}



grahp.h

Code:
#ifndef GRAPH_H_
#define GRAPH_H_

#include <iostream>
#include <string>
#include <vector>

using namespace std;

//max number of nodes

class GraphNode
{
private:
    vector<int> adjacencyList; //list of indices of nodes connected to this node
    int numAdjacentNodes;  //number of adjacent nodes
    int label;
    double x,y,z; //x,y,z as in a point in 3-d space.

public:
    GraphNode();
    GraphNode(int label, double x, double y, double z);

    //get and set functions
    vector<int> getAdjacencyList();
    int getNumAdjacentNodes();

    void setLabel(int label);
    void setX(double x);
    void setY(double y);
    void setZ(double z);

    int getLabel();
    double getX();
    double getY();
    double getZ();

    //utility functions
    void addAdjacentNode(int label);
    void print();
    void distance();
};

class Graph
{
private:
    vector<GraphNode> nodes;  //an array of nodes to represent the graph
    int numNodes;
    vector<vector<double> > distance_matrix;
    vector<vector<int> > connects;

public:
    Graph();
    //get functions
    int getNumNodes();

    //utility functions
    void print();
    void addNode(GraphNode node);
    GraphNode getNodeAt(int index);
    void setNodeAt(int index, GraphNode node);

};

#endif /* GRAPH_H_ */



graph.cpp

Code:
#include "graph.h"

using namespace std;

GraphNode::GraphNode(int label, double x, double y, double z){
    setLabel(label); setX(x); setY(y); setZ(z);
}


vector<int> GraphNode::getAdjacencyList()
{
    //returns the complete adjacency list
    //return (*adjacencyList);
    return adjacencyList;
}

int GraphNode::getNumAdjacentNodes()
{
    //returns the number of adjacent nodes
    return numAdjacentNodes;
}

void GraphNode::setLabel(int label){this->label = label;}
void GraphNode::setX(double x){this->x = x;}
void GraphNode::setY(double y){this->y = y;}
void GraphNode::setZ(double z){this->z = z;}

int GraphNode::getLabel(){return label;}
double GraphNode::getX(){return x;}
double GraphNode::getY(){return y;}
double GraphNode::getZ(){return z;}

void GraphNode::addAdjacentNode(int label){
    adjacencyList.push_back(label);
    numAdjacentNodes++;
}

void GraphNode::print()
{
    cout << "\t--- Node " << label << " ---"<<endl;
    cout << "Coordinate:\t\t"
         << getX() << " "
         << getY() << " "
         << getZ() << " "
         << endl;

    cout << "Adjacent Nodes:\t";
    for (int count = 0; count < numAdjacentNodes; count++)
    {
        //cout << adjacencyList[count];
        if (count != numAdjacentNodes - 1)
        {
            cout << ", ";
        }
    }
    cout << endl;
}

void GraphNode::distance(){
    double x_p, y_p, z_p;

/*    for(int i= 1; i <= nodeList.size(); ++i)
    {
        for(int j = 1; j <= nodeList.size(); ++j)
        {
            if (i == j)
            {
                distance_matrix[i][j] = 0;
            }
            else
            {
                x_p = nodeList[i].getX() - nodeList[j].getX();
                y_p = nodeList[i].getY() - nodeList[j].getY();
                z_p = nodeList[i].getZ() - nodeList[j].getZ();
                this->distance_matrix[i][j] = (x_p * x_p) + (y_p * y_p ) + ( z_p * z_p );
            }
        }
    }*/
}

int Graph::getNumNodes()
{
    //returns the number of nodes in the graph
    return numNodes;
}

void Graph::addNode(GraphNode node)
{
        nodes.push_back(node);
        numNodes++;
}

GraphNode Graph::getNodeAt(int index)
{
    if ((index < numNodes) && (index >= 0))
    {
        return nodes[index];
    }
    else
    {
        //invalid index specified
//        return GraphNode();  //return an 'empty' node
    }
}

void Graph::print()
{
    for (int count = 0; count < numNodes; count++)
    {
        //cout << "\t--- Node " << count << " ---"<<endl;
        nodes[count].print();
        cout << endl << endl;
    }
}

void Graph::setNodeAt(int index, GraphNode node)
{
    //assignments are only allowed into existing locations
    if ((index < numNodes) && (index >= 0))
    {
        nodes[index] = node;
    }
}



Wie bekommt man das Programm zum laufen?

Viele Grüße
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
001
17.05.2009, 08:23 Uhr
~f.-th.
Gast


Da du ja Zeiger übergibst:

C++:
Graph *graph = new Graph();



Dann werden dir weitere Fehler angezeigt, die du auch noch in Angriff nehmen kannst

MfG f.-th.
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
002
17.05.2009, 08:52 Uhr
xyz



Habe es geändert zu Graph *graph = new Graph();

Code:
../main.cpp: In function 'int main()':
../main.cpp:32: error: conversion from 'GraphNode*' to non-scalar type 'GraphNode' requested
../main.cpp:33: error: request for member 'addNode' in 'graph', which is of non-class type 'Graph*'
../main.cpp:37: error: request for member 'getNumNodes' in 'graph', which is of non-class type 'Graph*'
../main.cpp:38: error: request for member 'getNumNodes' in 'graph', which is of non-class type 'Graph*'
../main.cpp:39: error: request for member 'getNodeAt' in 'graph', which is of non-class type 'Graph*'
../main.cpp:39: error: request for member 'getNodeAt' in 'graph', which is of non-class type 'Graph*'
../main.cpp:40: error: request for member 'getNodeAt' in 'graph', which is of non-class type 'Graph*'
../main.cpp:40: error: request for member 'getNodeAt' in 'graph', which is of non-class type 'Graph*'
../main.cpp:46: error: request for member 'print' in 'graph', which is of non-class type 'Graph*'
make: *** [main.o] Error 1



Leider weiss ich nicht wie man die neuen Fehler behebt.
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
003
17.05.2009, 09:44 Uhr
0xdeadbeef
Gott
(Operator)



C++:
Graph graph = new Graph();


muss

C++:
Graph graph;


heißen, dann kannstes mit . statt -> benutzen, und es wird automatisch am Ende von main zerstört. Übrigens muss alles, was mit new angefordert wird, nachher mit delete wieder freigegeben werden, sonst hast du Speicherlecks.
--
Einfachheit ist Voraussetzung für Zuverlässigkeit.
-- Edsger Wybe Dijkstra
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
004
17.05.2009, 09:57 Uhr
xyz



Mit

Code:
Graph graph;

bekomme ich immer noch ein Fehler:

Code:
make all
Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o"main.o" "../main.cpp"
../main.cpp: In function 'int main()':
../main.cpp:32: error: conversion from 'GraphNode*' to non-scalar type 'GraphNode' requested
make: *** [main.o] Error 1



Wie kann ich diesen Fehler noch beheben?

Dieser Post wurde am 17.05.2009 um 09:58 Uhr von xyz editiert.
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
005
17.05.2009, 10:21 Uhr
TOSHMAX



Das ist das gleiche Problem wie mit Graph:
Einfach

C++:
GraphNode tempNode = new GraphNode(s_nodeTmp.label,
                                           s_nodeTmp.x,
                                           s_nodeTmp.y,
                                           s_nodeTmp.z);

zu

C++:
GraphNode tempNode(s_nodeTmp.label,
                                           s_nodeTmp.x,
                                           s_nodeTmp.y,
                                           s_nodeTmp.z);
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
006
17.05.2009, 10:53 Uhr
xyz



Habe es geändert in:

Code:
GraphNode tempNode(s_nodeTmp.label,
                                           s_nodeTmp.x,
                                           s_nodeTmp.y,
                                           s_nodeTmp.z);



Leider bekomme ich jetzt diese Fehlermeldung:

Code:
./main.o: In function `main':
../main.cpp:22: undefined reference to `Graph::Graph()'
collect2: ld returned 1 exit status
make: *** [graph3d] Error 1



In Zeile 22 steht dies:

Code:
Graph graph;



Wie kann ich dieses Problem lösen?

Dieser Post wurde am 17.05.2009 um 11:02 Uhr von xyz editiert.
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
007
17.05.2009, 19:26 Uhr
0xdeadbeef
Gott
(Operator)


Indem du den Konstruktor für Graph nicht nur deklarierst, sondern auch definierst.
--
Einfachheit ist Voraussetzung für Zuverlässigkeit.
-- Edsger Wybe Dijkstra
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
008
19.05.2009, 06:25 Uhr
xyz



Danke, jetzt lässt es sich kompilieren. Leider bekomme ich nur diese Ausgabe:

Code:
    --- Node 39 ---
Coordinate:        18.378, 59.466, 13.185
Adjacent Nodes:    0

    --- Node 40 ---
Coordinate:        14.27, 54.556, 18.227
Adjacent Nodes:    0

    --- Node 41 ---
Coordinate:        30.119, 51.333, 33.732
Adjacent Nodes:    0

    --- Node 43 ---
Coordinate:        26.326, 51.1, 21.154
Adjacent Nodes:    0

    --- Node 44 ---
Coordinate:        23.764, 55.05, 18.338
Adjacent Nodes:    0

    --- Node 45 ---
Coordinate:        17.697, 75.884, 26.178
Adjacent Nodes:    0

    --- Node 48 ---
Coordinate:        22.351, 66.269, 28.727
Adjacent Nodes:    0

    --- Node 49 ---
Coordinate:        19.98, 63.046, 33.535
Adjacent Nodes:    0



Anscheint wird die numAdjacentNodes Variable in GraphNode::addAdjacentNode(int label) nicht erhöht und somit wird wohl vector<int> adjacencyList nicht ausgeben.

Hier ist der aktuelle Code:
main.cpp

Code:
#include "graph.h"
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

struct s_node {
    int label;
    double x, y, z;
};

istream &operator>>(istream &in, s_node &n) {
    return in >> n.label >> n.x >> n.y >> n.z;
}

int main() {
    //make an empty graph, and an empty temporary graph node
    Graph *graph = new Graph();
    s_node s_nodeTmp;

    ifstream in("template.txt");
    //construct the nodes
    while(in >> s_nodeTmp) {
        //GraphNode *tempNode = new GraphNode(s_nodeTmp.label,
        GraphNode tempNode (s_nodeTmp.label,
                                           s_nodeTmp.x,
                                           s_nodeTmp.y,
                                           s_nodeTmp.z);
        graph->addNode(tempNode);
        //delete tempNode;
    }
    //construct the adjency
    for (int i = 0; i < graph->getNumNodes(); i++) {
        for (int j = 0; j < graph->getNumNodes(); j++) {
            if (graph->getNodeAt(i).getLabel() != graph->getNodeAt(j).getLabel()){
                graph->getNodeAt(i).addAdjacentNode(graph->getNodeAt(j).getLabel());
            }
        }
    }

    //print the graph
    //cout << "printing..." << endl;
    graph->print();
    delete graph;
    return 0;
}



graph.h

Code:
#ifndef GRAPH_H_
#define GRAPH_H_

#include <iostream>
#include <string>
#include <vector>

using namespace std;

//max number of nodes

class GraphNode
{
private:
    vector<int> adjacencyList; //list of indices of nodes connected to this node
    int numAdjacentNodes;  //number of adjacent nodes
    int label;
    double x,y,z; //x,y,z as in a point in 3-d space.

public:
    GraphNode(int label, double x, double y, double z);

    //get and set functions
    vector<int> getAdjacencyList();
    int getNumAdjacentNodes();

    void setLabel(int label);
    void setX(double x);
    void setY(double y);
    void setZ(double z);

    int getLabel();
    double getX();
    double getY();
    double getZ();

    //utility functions
    void addAdjacentNode(int label);
    void print();
    void distance();
};

class Graph
{
private:
    vector<GraphNode> nodes;  //an array of nodes to represent the graph
    int numNodes;
    vector<vector<double> > distance_matrix;
    vector<vector<int> > connects;

public:
    //get functions
    int getNumNodes();

    //utility functions
    void print();
    void addNode(GraphNode node);
    GraphNode getNodeAt(int index);
    void setNodeAt(int index, GraphNode node);

};

#endif /* GRAPH_H_ */



graph.cpp

Code:
#include "graph.h"

using namespace std;

GraphNode::GraphNode(int label, double x, double y, double z){
    setLabel(label); setX(x); setY(y); setZ(z);
    numAdjacentNodes = 0;
}


vector<int> GraphNode::getAdjacencyList()
{
    //returns the complete adjacency list
    //return (*adjacencyList);
    return adjacencyList;
}

int GraphNode::getNumAdjacentNodes()
{
    //returns the number of adjacent nodes
    return numAdjacentNodes;
}

void GraphNode::setLabel(int label){this->label = label;}
void GraphNode::setX(double x){this->x = x;}
void GraphNode::setY(double y){this->y = y;}
void GraphNode::setZ(double z){this->z = z;}

int GraphNode::getLabel(){return label;}
double GraphNode::getX(){return x;}
double GraphNode::getY(){return y;}
double GraphNode::getZ(){return z;}

void GraphNode::addAdjacentNode(int label){
    //cout << label << endl;
    adjacencyList.push_back(label);
    numAdjacentNodes++;
    cout << numAdjacentNodes << '\n';
}

void GraphNode::print()
{
    cout << "\t--- Node " << label << " ---"<<endl;
    cout << "Coordinate:\t\t"
         << getX() << ", "
         << getY() << ", "
         << getZ() << " "
         << endl;

    cout << "Adjacent Nodes:\t";
    cout << numAdjacentNodes << '\n';
    for (int count = 0; count < numAdjacentNodes; count++)
    {
        cout << adjacencyList[count];
        if (count != numAdjacentNodes - 1)
        {
            cout << ", ";
        }
    }
    cout << endl;
}

void GraphNode::distance(){
    double x_p, y_p, z_p;

/*    for(int i= 1; i <= nodeList.size(); ++i)
    {
        for(int j = 1; j <= nodeList.size(); ++j)
        {
            if (i == j)
            {
                distance_matrix[i][j] = 0;
            }
            else
            {
                x_p = nodeList[i].getX() - nodeList[j].getX();
                y_p = nodeList[i].getY() - nodeList[j].getY();
                z_p = nodeList[i].getZ() - nodeList[j].getZ();
                this->distance_matrix[i][j] = (x_p * x_p) + (y_p * y_p ) + ( z_p * z_p );
            }
        }
    }*/
}

int Graph::getNumNodes()
{
    //returns the number of nodes in the graph
    return numNodes;
}

void Graph::addNode(GraphNode node)
{
        nodes.push_back(node);
        numNodes++;
}

GraphNode Graph::getNodeAt(int index)
{
    if ((index < numNodes) && (index >= 0))
    {
        return nodes[index];
    }
    else
    {
        //invalid index specified
//        return GraphNode();  //return an 'empty' node
    }
}

void Graph::print()
{
    for (int count = 0; count < numNodes; count++)
    {
        nodes[count].print();
        cout << endl << endl;
    }
}

void Graph::setNodeAt(int index, GraphNode node)
{
    //assignments are only allowed into existing locations
    if ((index < numNodes) && (index >= 0))
    {
        nodes[index] = node;
    }
}



Warum wird die numAdjacentNodes Variable in GraphNode::addAdjacentNode(int label) nicht erhöht oder gibt es ein anderes problem warum vector<int> adjacencyList nicht ausgeben wird?
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
009
19.05.2009, 07:04 Uhr
0xdeadbeef
Gott
(Operator)


Das schon, aber addAdjacentNode wird nie aufgerufen.

Außerdem...warum benutzt du eigene Zähler, wenn std::vector doch eine size()-Methode hat?
--
Einfachheit ist Voraussetzung für Zuverlässigkeit.
-- Edsger Wybe Dijkstra
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
Seiten: > 1 < [ 2 ]     [ C / C++ (ANSI-Standard) ]  


ThWBoard 2.73 FloSoft-Edition
© by Paul Baecher & Felix Gonschorek (www.thwboard.de)

Anpassungen des Forums
© by Flo-Soft (www.flo-soft.de)

Sie sind Besucher: