Stanford cs106L赋值1:Graphs Simple Graph.cpp语法错误

时间:2014-05-23 16:38:49

标签: c++ qt c++11

当我尝试编译以下代码时,出现了一些语法错误。这是来自Assignment 1 CS106L Stanford。我找不到任何遗失的carachters。 任何人都可以帮我解决这个问题吗?我认为我遗漏了一些基本的东西,


  cl : Command line warning D9002 : ignoring unknown option '-std=c++11'
    SimpleGraph.cpp
 ..\GraphViz\SimpleGraph.cpp(50) : error C2059: syntax error : '{'
..\GraphViz\SimpleGraph.cpp(50) : error C2143: syntax error : missing ';' before '{'
..\GraphViz\SimpleGraph.cpp(50) : error C2143: syntax error : missing ';' before '}'
..\GraphViz\SimpleGraph.cpp(53) : error C2143: syntax error : missing ',' before ':'
..\GraphViz\SimpleGraph.cpp(53) : error C2530: 'e' : references must be initialized
..\GraphViz\SimpleGraph.cpp(53) : error C2143: syntax error : missing ';' before '{'
..\GraphViz\SimpleGraph.cpp(58) : error C2143: syntax error : missing ',' before ':'
..\GraphViz\SimpleGraph.cpp(58) : error C2530: 'n' : references must be initialized
..\GraphViz\SimpleGraph.cpp(58) : error C2143: syntax error : missing ';' before '{'
..\GraphViz\SimpleGraph.cpp(50) : error C2059: syntax error : '{'
..\GraphViz\SimpleGraph.cpp(50) : error C2143: syntax error : missing ';' before '{'
..\GraphViz\SimpleGraph.cpp(50) : error C2143: syntax error : missing ';' before '}'

#include <iostream>
// Bugfix: Disable C++11 feature macros on Mac/libstdc++ to fool Qt into not using C++11 headers
#ifdef __APPLE__
#undef __cplusplus
#define __cplusplus 200303L
#undef __GXX_EXPERIMENTAL_CXX0X__
#endif

#include <QtGui>
#include <QWidget>
#include <QApplication>
#include <algorithm>
#include <QCoreApplication>
#include <QObject>
#include <QSemaphore>

#include "SimpleGraph.h"
#undef main

MyWidget& MyWidget::getInstance() {
    static MyWidget instance;
    return instance;
}

/* global semaphore to ensure only one update call at a time.
 * Prevents overload of update calls */
QSemaphore semaphore;

void InitGraphVisualizer(SimpleGraph & userGraph) {
    MyWidget& g = MyWidget::getInstance();
    QObject::connect(&userGraph, SIGNAL(graphUpdated(SimpleGraph)),
                     &g, SLOT(repaint()));
}

void MyWidget::paintEvent(QPaintEvent *event) {
    Q_UNUSED(event);
    QPainter painter(this);
    if (!userGraph.nodes.empty()) {
        SimpleGraph copy;
        copy.nodes = userGraph.nodes;
        copy.edges = userGraph.edges;        auto getX = [](const Node& a, const Node& b) {return a.x < b.x;};
        double maxX = std::max_element(copy.nodes.begin(), copy.nodes.end(), getX)->x;
        double minX = std::min_element(copy.nodes.begin(), copy.nodes.end(), getX)->x;

        auto getY = [](const Node& a, const Node& b) { return a.y < b.y;};
        double maxY = std::max_element(copy.nodes.begin(), copy.nodes.end(), getY)->y;
        double minY = std::min_element(copy.nodes.begin(), copy.nodes.end(), getY)->y;

        auto scale = [maxX, maxY, minX, minY](const Node& a) -> Node {
            return {(a.x - minX) * 590 / (-minX + maxX) + 5, (a.y - minY) * 590 / (-minY + maxY) + 5};};
        painter.setBrush(Qt::blue);
        std::transform(copy.nodes.begin(), copy.nodes.end(), copy.nodes.begin(), scale);
        for (Edge & e : copy.edges) {
            Node start = copy.nodes[e.start];
            Node end = copy.nodes[e.end];
            painter.drawLine(start.x, start.y, end.x, end.y);
        }
        for (Node & n : copy.nodes) {
            painter.drawEllipse(n.x - 5, n.y - 5, 10, 10);
        }
    }
    //last_run = QTime::currentTime();
    semaphore.release();
}

class WorkerThread : public QThread {
    void run() {
        int _userMain();
        _userMain();

    }
};

int main(int argc, char **argv) {
    QApplication app(argc, argv);
    MyWidget & myWidget = MyWidget::getInstance();
    myWidget.resize(600, 600);
    myWidget.show();
    qRegisterMetaType<SimpleGraph>(); //allows use of SimpleGraph in signals/slots
    WorkerThread x;
    x.start();
    return app.exec();
}

void SimpleGraph::drawGraph(SimpleGraph &graph) {
    if (!semaphore.tryAcquire()) return;
    MyWidget& m = MyWidget::getInstance();
    m.userGraph.nodes = graph.nodes;
    m.userGraph.edges = graph.edges;
    emit graphUpdated(graph);
}

void DrawGraph(SimpleGraph& userGraph) {
    userGraph.drawGraph(userGraph);
}

1 个答案:

答案 0 :(得分:1)

  

cl:命令行警告D9002:忽略未知选项&#39; -std = c ++ 11&#39;

首先,编译器告诉您没有启用C ++ 11的选项。如果编译器支持C ++ 11,则应在项目文件中使用它。如果编译器不支持基于范围的循环,那么你将无法使用该编译器。

无论如何,它的qmake选项是:

CONFIG += c++11

qmake会透明地在后台找出你的详细信息。基本上,cl.exe默认会识别它们,因此无需在该平台上明确传递-std=c++11

如果你没碰巧使用qmake,你可以删除它。

其次,你也有语法问题:

auto scale = [maxX, maxY, minX, minY](const Node& a) -> Node {
    return {(a.x - minX) * 590 / (-minX + maxX) + 5, (a.y - minY) * 590 / (-minY + maxY) + 5};
};

如果编译器不支持,则返回语句的语法不正确。

第三,对于基于循环的范围,您可能希望将const放在那里,因为您不修改。你也可以在那里使用auto,如下所示:

for (auto const & e : copy.edges) {

for (auto const & n : copy.nodes) {

请在此处查看此表以查看编译器支持的内容:

http://blogs.msdn.com/b/vcblog/archive/2010/04/06/c-0x-core-language-features-in-vc10-the-table.aspx