我正在尝试使用以下命令在C++中编译2个类:
g++ Cat.cpp Cat_main.cpp -o Cat
但是我收到以下错误:
Cat_main.cpp:10:10: error: variable ‘Cat Joey’ has initializer but incomplete type
有人可以向我解释这意味着什么吗?我的文件基本上是创建一个class(Cat.cpp)并创建一个实例(Cat_main.cpp).这是我的源代码:
Cat.cpp:
#include <iostream>
#include <string>
class Cat;
using namespace std;
int main()
{
Cat Joey("Joey");
Joey.Meow();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Cat_main.cpp:
#include <iostream>
#include <string>
using namespace std;
class Cat
{
public:
Cat(string str);
// Variables
string name;
// Functions
void Meow();
};
Cat::Cat(string str)
{
this->name = str;
}
void Cat::Meow()
{
cout << "Meow!" << endl;
return; …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下架构在c ++中编写内容:
App - > Core(.so)< - 插件(.so)
对于linux,mac和windows.Core隐式链接到App,而插件显式链接到dlopen/LoadLibrary到App.我遇到的问题:
有人可以给我一些不同平台的解释和说明吗?我知道在这里问他们所有人似乎都很懒,但我真的找不到这个问题的系统答案.
我在entry_point.cpp中为插件做了什么:
#include "raw_space.hpp"
#include <gamustard/gamustard.hpp>
using namespace Gamustard;
using namespace std;
namespace
{
struct GAMUSTARD_PUBLIC_API RawSpacePlugin : public Plugin
{
RawSpacePlugin(void):identifier_("com.gamustard.engine.space.RawSpacePlugin")
{
}
virtual string const& getIdentifier(void) const
{
return identifier_;
}
virtual SmartPtr<Object> createObject(std::string const& name) const
{
if(name == "RawSpace")
{
Object* obj = NEW_EX RawSpaceImp::RawSpace;
Space* space = dynamic_cast<Space*>(obj);
Log::instance().log(Log::LOG_DEBUG, "createObject: %x -> %x.", obj, space);
return SmartPtr<Object>(obj);
}
return SmartPtr<Object>();
}
private: …Run Code Online (Sandbox Code Playgroud)我代表一位朋友发帖,因为我觉得这很有趣:
取字符串"abb".通过遗漏少于字符串长度的任意数量的字母,我们最终得到7个字符串.
abb ab ab bb abb
其中4个是回文.
同样对于字符串
"hihellolookhavealookatthispalindromexxqwertyuiopasdfghjklzxcvbnmmnbvcxzlkjhgfdsapoiuytrewqxxsoundsfamiliardoesit"
(长度为112弦)2 ^ 112 - 可以形成1个弦.
其中有多少是回文?
下面是他的实现(在C++中,C也很好).用很长的词来说它很慢; 他想知道什么是最快的算法(我也很好奇:D).
#include <iostream>
#include <cstring>
using namespace std;
void find_palindrome(const char* str, const char* max, long& count)
{
for(const char* begin = str; begin < max; begin++) {
count++;
const char* end = strchr(begin + 1, *begin);
while(end != NULL) {
count++;
find_palindrome(begin + 1, end, count);
end = strchr(end + 1, *begin);
}
}
}
int main(int argc, char *argv[])
{
const …Run Code Online (Sandbox Code Playgroud) 编译程序后,我遇到错误
invalid operands of types int and double to binary 'operator%' at line
"newnum1 = two % (double)10.0;"
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
#include<iostream>
#include<math>
using namespace std;
int main()
{
int num;
double two = 1;
double newnum, newnum1;
newnum = newnum1 = 0;
for(num = 1; num <= 50; num++)
{
two = two * 2;
}
newnum1 = two % (double)10.0;
newnum = newnum + newnum1;
cout << two << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud) 在这个相当基本的C++代码片段中,涉及随机数生成:
include <iostream>
using namespace std;
int main() {
cout << (rand() % 100);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么我总是得到41的输出?我试图让它输出0到100之间的一些随机数.也许我不理解rand函数是如何工作的?
我遇到了以下计算大型因子(数字大到100)的程序..谁能解释一下这个算法中使用的基本思想?我只需要知道在计算阶乘时实现的数学.
#include <cmath>
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
unsigned int d;
unsigned char *a;
unsigned int j, n, q, z, t;
int i,arr[101],f;
double p;
cin>>n;
p = 0.0;
for(j = 2; j <= n; j++)
p += log10(j);
d = (int)p + 1;
a = new unsigned char[d];
for (i = 1; i < d; i++)
a[i] = 0; //initialize
a[0] = 1;
p = 0.0;
for (j = 2; j <= n; j++) …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Protocol Buffers 2.6.1和GNU GCC 5.1.0(在Ubuntu 14.10上)构建简单的测试应用程序,并且我得到以下错误:
/home/ragnar/cpp-tools/gcc-linux/bin/g++ -c "/home/ragnar/cpp-projects/gprotobuf_test/main.cpp" -g -O0 -Wall -o ./Debug/main.cpp.o -I. -I/home/ragnar/cpp-tools/libs/linux64/protobuf/include -I.
/home/ragnar/cpp-tools/gcc-linux/bin/g++ -c "/home/ragnar/cpp-projects/gprotobuf_test/messages.pb.cc" -g -O0 -Wall -o ./Debug/messages.pb.cc.o -I. -I/home/ragnar/cpp-tools/libs/linux64/protobuf/include -I.
/home/ragnar/cpp-tools/gcc-linux/bin/g++ -o ./Debug/gprotobuf_test @"gprotobuf_test.txt" -L. -L/home/ragnar/cpp-tools/libs/linux64/protobuf/lib -lprotobuf
./Debug/main.cpp.o: In function google::protobuf::internal::GetEmptyStringAlreadyInited[abi:cxx11]():
/home/ragnar/cpp-tools/libs/linux64/protobuf/include/google/protobuf/generated_message_util.h:80:
undefined reference to google::protobuf::internal::empty_string_[abi:cxx11]
/home/ragnar/cpp-tools/libs/linux64/protobuf/include/google/protobuf/generated_message_util.h:81:
undefined reference to google::protobuf::internal::empty_string_[abi:cxx11]
./Debug/messages.pb.cc.o: In function protobuf_AssignDesc_messages_2eproto():
/home/ragnar/cpp-projects/gprotobuf_test/messages.pb.cc:32:
undefined reference to google::protobuf::DescriptorPool::FindFileByName(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const
./Debug/messages.pb.cc.o: In function protobuf_AddDesc_messages_2eproto():
/home/ragnar/cpp-projects/gprotobuf_test/messages.pb.cc:83:
undefined reference to google::protobuf::MessageFactory::InternalRegisterGeneratedFile(char const*, void (*)(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&))
./Debug/messages.pb.cc.o: …Run Code Online (Sandbox Code Playgroud) 我试图理解C/C++中的编译和链接过程.我知道源文件首先由编译器转换为目标文件.然后,链接器从目标文件生成库或可执行文件.
我试图首先读取目标文件中的信息.这是我为实验编写的程序.
func.h
#include <iostream>
void beautifulprint(char *str);
Run Code Online (Sandbox Code Playgroud)
func.cpp
#include "stdafx.h"
#include "func.h"
using namespace std;
void beautifulprint(char *str) {
cout << "*** " << str << " ***" << endl;
}
Run Code Online (Sandbox Code Playgroud)
TestApp.cpp
#include "stdafx.h"
#include "func.h"
int _tmain(int argc, _TCHAR* argv[])
{
beautifulprint("Hello, world!");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在VS 2010中构建项目后,我得到了func.obj.我假设在func.obj中的某个地方应该有一个beautifulprint函数的引用.我为func.obj的调试版和发行版运行了以下版本
dumpbin /HEADERS func.obj > funchead.txt
Run Code Online (Sandbox Code Playgroud)
以下是输出.
调试版本(不包括完整输出,因为它非常大)
...
SECTION HEADER #41
.text name
0 physical address
0 virtual address
78 size of raw data
5B94 file pointer to raw data …Run Code Online (Sandbox Code Playgroud) 我从http://curl.haxx.se/得到一个小程序 ,而我运行它总是打印网页如何禁用打印功能
#include <iostream>
#include <curl/curl.h>
using namespace std;
int main() {
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://google.com");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION,1);
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我收到以下错误,我不知道为什么.
{ 1, 1}: On entry to PDPOTRF parameter number 2 had an illegal value
{ 1, 0}: On entry to PDPOTRF parameter number 2 had an illegal value
{ 0, 1}: On entry to PDPOTRF parameter number 2 had an illegal value
{ 0, 0}: On entry to PDPOTRF parameter number 2 had an illegal value
info < 0: If the i-th argument is an array and the j-entry had an illegal value, then INFO = -(i*100+j), if the …Run Code Online (Sandbox Code Playgroud)