当我有以下代码时:
#include <iostream>
#include <queue>
using namespace std;
struct Card
{
enum suit {CLUBS, DIAMONDS, HEARTS, SPADES};
suit Suit;
int rank;
};
class Deck
{
queue<Card> deck;
public:
//create the deck of cards
for(int i = 0; i <= 4; i++)
{
for(int j = 0; j <= 13; j++)
{
Card temp = {i, j}
}
}
};
int main()
{
std::cout < "Hello\n";
}
这包含一些不是问题的错误。问题是我收到的错误消息。第一部分是可以理解的:
> Executing task: g++ -g main.cpp <
main.cpp:19:5: error: expected unqualified-id before 'for'
for(int i = 0; i <= 4; i++)
^~~
main.cpp:19:20: error: 'i' does not name a type
for(int i = 0; i <= 4; i++)
^
main.cpp:19:28: error: 'i' does not name a type
for(int i = 0; i <= 4; i++)
^
然而,第二部分似乎试图用Haskell编译器编译代码。
main.cpp: In function 'int main()':
main.cpp:31:15: error: no match for 'operator<' (operand types are
'std::ostream {aka std::basic_ostream<char>}' and 'const char [7]')
std::cout < "Hello\n";
~~~~~~~~~~^~~~~~~~~~~
In file included from C:/Program Files/Haskell
Platform/8.2.2/mingw/include/c++/6.2.0/bits/stl_algobase.h:64:0,
from C:/Program Files/Haskell
Platform/8.2.2/mingw/include/c++/6.2.0/bits/char_traits.h:39,
from C:/Program Files/Haskell
Platform/8.2.2/mingw/include/c++/6.2.0/ios:40,
from C:/Program Files/Haskell
Platform/8.2.2/mingw/include/c++/6.2.0/ostream:38,
from C:/Program Files/Haskell
Platform/8.2.2/mingw/include/c++/6.2.0/iostream:39,
from main.cpp:1:
C:/Program Files/Haskell
Platform/8.2.2/mingw/include/c++/6.2.0/bits/stl_pair.h:369:5: note:
candidate: template<class _T1, class _T2> constexpr bool
std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
^~~~~~~~
这种情况持续了一段时间,因为c ++程序并不是完全有效的Haskell代码。我想知道如何解决这个问题,因为它使调试成为一个大问题。我正在使用VSCode,虽然我可以共享我的c_cpp_properties.json文件但我已经确定我做对了,我不想再提问。
答案 0 :(得分:2)
它是c ++编译器,而不是Haskell&#39。 正如其他人在评论中指出的那样,你不能在类定义中提出循环(或任何可执行代码)。
让我们尝试理解第二条错误消息:
main.cpp: In function 'int main()':
main.cpp:31:15: error: no match for 'operator<' (operand types are
'std::ostream {aka std::basic_ostream<char>}' and 'const char [7]')
std::cout < "Hello\n";
~~~~~~~~~~^~~~~~~~~~~
通过撰写std::cout < "Hello\n";
,您说要将std::cout
对象std::ostream
与const char [7]
(字符串)进行比较。编译器告诉你,它不知道如何做到这一点。因为正式地你要求operator<
功能但它找不到它。
但它确实发现了以下两个功能:
template<class _T1, class _T2> constexpr bool
std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
编译器认为也许你打算打电话给他们,你知道也许你输了一个错字。编译器还告诉你它们位于何处:
C:/Program Files/Haskell
Platform/8.2.2/mingw/include/c++/6.2.0/bits/stl_pair.h:369:5: note:
消息的中间部分还告诉您编译器是如何找到它们的,因为它们包含在main.cpp
中:(从下到上阅读)
In file included from C:/Program Files/Haskell
Platform/8.2.2/mingw/include/c++/6.2.0/bits/stl_algobase.h:64:0,
from C:/Program Files/Haskell
Platform/8.2.2/mingw/include/c++/6.2.0/bits/char_traits.h:39,
from C:/Program Files/Haskell
Platform/8.2.2/mingw/include/c++/6.2.0/ios:40,
from C:/Program Files/Haskell
Platform/8.2.2/mingw/include/c++/6.2.0/ostream:38,
from C:/Program Files/Haskell
Platform/8.2.2/mingw/include/c++/6.2.0/iostream:39,
这些文件是Haskell目录中的一些原因,这就是全部。
C ++中的错误消息有时很长(特别是对于模板),但是如果你学会仔细阅读它们,你应该总是有足够的信息来修复你的代码,或者至少看看问题出在哪里。