我正在尝试使用g ++编译一些Microsoft Visual C++代码.现在我遇到了一个我真的无法理解的编译器错误.(简化)代码如下所示:
template<int X> struct A {
template<class Ret> static Ret call() {
return 0;
}
};
template<int X> struct B : A<X> {
int f() {
return A<X>::call<int>();
}
};
Run Code Online (Sandbox Code Playgroud)
当我尝试用g ++(版本4.4.5)编译它时,我收到以下错误:
main.cpp: In member function int B<X>::f():
main.cpp:16: error: expected primary-expression before int
main.cpp:16: error: expected ; before int
main.cpp:16: error: expected unqualified-id before > token
Run Code Online (Sandbox Code Playgroud)
如果我从方法A :: call中删除模板类型(Ret),代码编译就好了.任何人都可以看到这里有什么不对吗?
谢谢!
最近更新了clang(以及xcode和开发人员工具)并运行了一个简单的程序来查看它是否支持c ++ 11.看起来像这样:
#include <iostream>
using namespace std;
int main()
{
string my_array[5] = {"one", "two", "three"};
for (string &x : my_array)
cout << x << endl;
}
Run Code Online (Sandbox Code Playgroud)
像这样在终端编译:
clang++ -std=c++11 -stdlib=libc++ -Weverything main.cpp
Run Code Online (Sandbox Code Playgroud)
并得到这个警告:
main.cpp:17:20: warning: range-based for loop is incompatible with C++98
[-Wc++98-compat]
for (string &x : my_array)
Run Code Online (Sandbox Code Playgroud)
但它仍然生成一个可执行文件并按预期运行.为什么会产生这个错误?
在发布此帖子之前,我尝试了一些Google搜索,但说实话,我不知道要搜索什么.我有一个C++项目,并且很高兴使用GNU编译器(g ++).今天我尝试使用clang ++进行编译并获得了段错误.
好的,好的,我可以解决这个问题.在仔细阅读我的代码并打印一些东西之后,我就能解决问题了.然而,这个解决方案让我感到非常麻烦和困惑.
这是情况:我使用的是树状数据结构,它存储了一个名为Ligament的类,但我将它存储在std :: vector中.我这样做是通过存储一个"children"的向量,它实际上只是向量中父对象和子对象之间的整数偏移量.通过这种方式,我可以使用this指针访问子节点,即
child = this[offset];
Run Code Online (Sandbox Code Playgroud)
但是,这一点都不重要.这是这个问题:我有一个Ligament :: addChild(int)函数,它接受一个整数并将其推送到作为Ligament成员的向量的后面:
void Ligament::addChild(uint32_t offset){
children.push_back(offset);
}
Run Code Online (Sandbox Code Playgroud)
很简单的东西.一般情况下,我向addChild传递一个从名为fill的递归函数返回的参数:
//starting at root
uint32_t fill(vector<Ligament>& lVec, TiXmlElement * el){
//store current size here, as size changes during recursion
uint32_t curIdx = lVec.size();
lVec.push_back(createLigament());
//Add all of this Ligament's children
TiXmlElement * i = el->FirstChildElement("drawable");
for (; i; i=i->NextSiblingElement("drawable")){
uint32_t tmp = fill(lVec, i) - curIdx;
lVec[curIdx].addChild(tmp);
//Does not work in clang++, but does in g++
//lVec[curIdx].addChild(fill(lVec,i)-curIdx);
}
//return the ligament's index …Run Code Online (Sandbox Code Playgroud) 在重构一个相当大的代码库的过程中,我的编译器想出了一个误解我的好方法.这是我所说的最简单的例子:
#include <iostream>
class Foo {
public:
virtual int get() = 0;
template <typename T> int get(int i) { return 4 + i; }
};
class Bar : public Foo {
public:
virtual int get() { return 3; }
};
int main(int argv, char **argc) {
Bar b;
std::cout << b.get<char>(7) << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Clang 3.6,gcc 4.7,gcc 4.8和gcc 4.9都将"b.get(7)"标记为"b.get"和"char"之间的比较运算符.
template-test.cpp: In function ‘int main(int, char**)’:
template-test.cpp:16:17: error: invalid use of non-static member function
std::cout << b.get<char>(7) << std::endl; …Run Code Online (Sandbox Code Playgroud) 我在OpenSUSE上有Qt-Creator.在其中有一个带有编译器'clang'的C++项目.我有'.pro'配置:
INCLUDEPATH += "/?Data_Disk??/Build/include/c++/v1/"
LIBS += "-stdlib=libc++"
LIBS += "/?Data_Disk??/Build/lib/libc++abi.a"
QMAKE_CXXFLAGS += -std=c++14 -stdlib=libc++
TARGET = testCpp
SOURCES += main.cpp
Run Code Online (Sandbox Code Playgroud)
当我编译时,我得到了这个输出:
clang++ -c -pipe -Qunused-arguments -std=c++14 -stdlib=libc++ -g -Wall -W -D_REENTRANT -DQT_WEBKIT -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/unsupported/linux-clang -I../testCpp -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I/?Data_Disk??/Build/include/c++/v1 -I. -I../testCpp -I. -o main.o ../testCpp/main.cpp
Run Code Online (Sandbox Code Playgroud)
我希望它是:
clang++ -c -pipe -Qunused-arguments -std=c++14 -stdlib=libc++ -g -Wall -W -D_REENTRANT -DQT_WEBKIT -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/?Data_Disk??/Build/include/c++/v1 -I/usr/share/qt4/mkspecs/unsupported/linux-clang -I../testCpp -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I. -I../testCpp -I. -o main.o ../testCpp/main.cpp
Run Code Online (Sandbox Code Playgroud)
要么:
clang++ -c -pipe -Qunused-arguments -std=c++14 …Run Code Online (Sandbox Code Playgroud) 我在使用MacOSX上的共享库编译代码时遇到了一些麻烦.在尝试在MacOSX上编译之前,我首先在Debian上编写了它.
这是代码:
test.hxx:
#ifndef TEST_HXX
#define TEST_HXX
namespace test
{
class CFoo
{
/* data */
public:
CFoo (){}
virtual ~CFoo (){}
void bar();
}; /* CFoo */
} /* namespace test */
#endif /* TEST_HXX */
Run Code Online (Sandbox Code Playgroud)
test.cxx:
#include <iostream>
#include "test.hxx"
void test::CFoo::bar()
{
std::cout << "Hello world!" << std::endl;
} /* bar() */
Run Code Online (Sandbox Code Playgroud)
other.hxx:
#ifndef OTHER_HXX
#define OTHER_HXX
namespace other
{
class CBar
{
public:
CBar (){}
virtual ~CBar (){}
void foo();
}; /* CBar */
} /* …Run Code Online (Sandbox Code Playgroud) 我有一段代码,它应该声明基础结构,然后声明继承它的模板结构.然后结构部分被证明.
#include <utility>
#include <iostream>
template<class A, class B>
struct Parent {
std::pair<A, B> m_pair;
void print() {
std::cout << m_pair.first << ", " << m_pair.second << "\n";
}
};
template <class A, class B>
struct Some : public Parent<A, B> {
Some(A a, B b) : Parent<A, B>({ {a, b} }) {}
void add() {
m_pair.first += m_pair.second;
}
};
template <class B>
struct Some<B, float> : public Parent<B, float> {
Some(B a, float b) : Parent<B, float>({ {a, …Run Code Online (Sandbox Code Playgroud) 所以我在android上使用clang ++和termux编译了一个简单的cpp程序,但是我无法运行该程序,出现以下错误:
$ ./execname
-bash: . /execname: Permission denied
Run Code Online (Sandbox Code Playgroud) 我正在使用Centos平台上的共享库和应用程序[clang ++,llvm3.9.0和libc ++],并且库和应用程序都会重载它们自己的operator new和operator delete.
除1例外,一切正常.在调用std :: string的copy构造函数时总是调用应用程序端的operator new:
这是senario:
std::string str1 ( "A very strange issue on CentOS using clang and libc++" ); //operator new of library side called.
std::string str2(str1); //operator new of application side called. WHY??
Run Code Online (Sandbox Code Playgroud)
对于库侧调用两种情况下的operator delete.
以下是运行以下代码时的日志:
====================================================
operator new in shared library
operator new called Application side
operator delete in shared library
operator delete in shared library
====================================================
Run Code Online (Sandbox Code Playgroud)
共享库侧操作员new和delete:
void * operator new ( size_t len ) throw ( std::bad_alloc …Run Code Online (Sandbox Code Playgroud) 我正在探索c ++ 17的新增内容.经过玩弄 std::variant,想用std::optional也用同样的例子.目前看到编译因以下错误而失败:
error: no viable conversion from returned value of type
'(lambda at ./html_parser.hpp:53:9)' to function return type 'Parser<char>' (aka
'std::__1::function<std::__1::optional<std::__1::pair<char, std::__1::basic_string<char> > >
(std::__1::basic_string<char>)>')
return [=](std::string& input) -> ParserResult<char> {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/acid/tools/include/c++/v1/functional:1627:5: note: candidate constructor not viable: no known conversion
from '(lambda at ./html_parser.hpp:53:9)' to 'std::nullptr_t' (aka 'nullptr_t') for 1st argument
function(nullptr_t) _NOEXCEPT : __f_(0) {}
^
/home/acid/tools/include/c++/v1/functional:1628:5: note: candidate constructor not viable: no known conversion
from '(lambda at ./html_parser.hpp:53:9)' to 'const
std::__1::function<std::__1::optional<std::__1::pair<char, …Run Code Online (Sandbox Code Playgroud)