2014-05-23 1 views
-2

다음 코드를 컴파일하려고하면 구문 오류가 발생합니다. 이것은 Assignment 1 CS106L Stanford의 것입니다. 나는 실종 된 캐러지를 찾지 못했습니다. 누구든지이 문제에 대해 도움을 줄 수 있습니까? 내 생각 there're 미안이없는 몇 가지 기본적인 것들,스탠포드 cs106L 할당 1 : 그래프 SimpleGraph.cpp Sintax 오류


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

'# 다음과 같이 당신은 또한 거기에 자동차를 사용할 수 있습니다 undef main' 뭐라고? – PaulMcKenzie

+0

제목에 sintax 오류가 있습니다. – Casey

+0

@PaulMcKenzie : 그와 같은 코드에서 C++ 11 기능을 수동으로 비활성화하는 것과 관련하여 엄청난 해킹처럼 보입니다. ;) – lpapp

답변

1

CL : 명령 줄 경고 D9002 : 알 수없는 옵션을 무시하고 '-std = C++ (11) '

우선 컴파일러는 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를 넣을 수 있습니다.

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

+0

이 코드는 다음과 같습니다. StarterCode – user3669666

+0

@Casey : 예, 이미 수정되었습니다. 아마 눈치 채지 못했을 것입니다. – lpapp

+0

http://msdn.microsoft.com/en-us/library/hh567368.aspx는 Microsoft 컴파일러에서 C++ 11 기능 지원을위한 최신 링크입니다. – Casey