我正在重构包含许多源文件的C++项目.当前的重构步骤包括将两个文件(例如,x.cpp和y.cpp)连接成一个较大的文件(比方说xy.cpp),其中一些代码被抛出,并且还添加了一些代码.
我想告诉我的版本控制系统(Perforce,在我的情况下)生成的文件基于两个以前的文件,因此将来,当我查看修订历史时xy.cpp,我也会看到所做的所有更改x.cpp和y.cpp.
Perforce支持重命名文件,因此如果y.cpp不存在,我会确切知道该怎么做.Perforce还支持合并,所以如果我有2个不同的版本,xy.cpp它可以从中创建一个版本.由此,我发现加入两个不同的文件是可能的(不确定); 但是,我搜索了一些关于Perforce和其他源代码控制系统的文档,并没有找到任何有用的东西.
我正在尝试做什么?
它是否有一个传统的名称(搜索"合并"或"加入"的文档是不成功的)?
我写了以下程序:
#include <stdio.h>
int main()
{
int i = 0;
for (; i < 4; i++)
{
printf("%i",i);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我使用gcc test.c -o test.o它编译它,然后使用它进行反汇编objdump -d -Mintel test.o.我得到的汇编代码(至少是相关部分)如下:
0804840c <main>:
804840c: 55 push ebp
804840d: 89 e5 mov ebp,esp
804840f: 83 e4 f0 and esp,0xfffffff0
8048412: 83 ec 20 sub esp,0x20
8048415: c7 44 24 1c 00 00 00 mov DWORD PTR [esp+0x1c],0x0
804841c: 00
804841d: eb 19 jmp 8048438 <main+0x2c>
804841f: 8b 44 24 …Run Code Online (Sandbox Code Playgroud) 我最近在一些正在开发的大型程序中发现了一个烦人的问题; 我想了解如何以最佳方式解决它.我将代码剪切到以下最小的示例.
#include <iostream>
using std::cin;
using std::cout;
class MagicNumbers
{
public:
static const int BIG = 100;
static const int SMALL = 10;
};
int main()
{
int choice;
cout << "How much stuff do you want?\n";
cin >> choice;
int stuff = (choice < 20) ? MagicNumbers::SMALL : MagicNumbers::BIG; // PROBLEM!
cout << "You got " << stuff << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当使用-O0或-O1进行编译时,我在gcc 4.1.2中遇到链接错误,但在使用-O2或-O3进行编译时一切正常.无论优化选项如何,它都可以使用MS Visual Studio 2005很好地链接.
test.cpp :(.text + 0xab):未定义的引用`MagicNumbers :: SMALL'
test.cpp :(.text + 0xb3):未定义引用`MagicNumbers …
阅读C++标准,我看到有"函数"类型和"函数指针"类型:
typedef int func(int); // function
typedef int (*pfunc)(int); // pointer to function
typedef func* pfunc; // same as above
Run Code Online (Sandbox Code Playgroud)
我从未见过在示例之外使用的函数类型(或者我可能没有认识到它们的用法?).一些例子:
func increase, decrease; // declares two functions
int increase(int), decrease(int); // same as above
int increase(int x) {return x + 1;} // cannot use the typedef when defining functions
int decrease(int x) {return x - 1;} // cannot use the typedef when defining functions
struct mystruct
{
func add, subtract, multiply; // declares three member functions
int member;
}; …Run Code Online (Sandbox Code Playgroud) 我已经使用了以下C++经验法则很长时间了:
如果类覆盖其基类中的函数,则应在基类中声明该函数
virtual.
我想我从这个规则中遇到了一个例外.为了判断这是否合理,或者指出我的设计存在缺陷,我问的是这个问题.我想得到例子或更好的规则.
编辑:我试着在这里描述我的用例,我明白我并不需要继承!
我想问一个普遍的问题.谢谢你的回答!
该函数可以按原样编译。auto在这里运作良好。但是这样一个函数的显式返回类型是什么?
auto rangeTest()
{
static const std::vector<int> vi{1,2,3,4,5,6,7,8,9,10};
auto rng = vi | ranges::view::remove_if([](int i){return i % 2 == 1;})
| ranges::view::transform([](int i){return std::to_string(i);});
return rng;
}
Run Code Online (Sandbox Code Playgroud) 我使用gcc(运行as g++)和GNU make.我使用gcc预编译头文件precompiled.h,创建precompiled.h.gch; Makefile中的以下行执行此操作:
# MYCCFLAGS is a list of command-line parameters, e.g. -g -O2 -DNDEBUG
precompiled.h.gch: precompiled.h
g++ $(MYCCFLAGS) -c $< -o $@
Run Code Online (Sandbox Code Playgroud)
一切都很好,直到我不得不运行g++不同的命令行参数.在这种情况下,即使precompiled.h.gch存在,也无法使用,编译速度会慢得多.在gcc文档中,我已经读过要处理这种情况,我必须创建一个目录precompiled.h.gch并将预编译的头文件放在那里,每个g++命令行参数都有一个文件.
所以现在我想知道我应该如何改变我的Makefile以告诉我用g++这种方式创建gch文件.也许我可以运行g++只是为了测试它是否可以使用precompiled.h.gch目录中的任何现有文件,如果没有,则生成具有唯一文件名的新预编译头.
gcc是否支持进行此类测试?
也许我可以用另一种方式实现我想要的东西?
有没有办法让IdeaVim在输入以外的模式下理解俄语布局?在我的.vimrc中我有
set keymap=russian-jcukenwin
Run Code Online (Sandbox Code Playgroud)
所以我可以通过按Ctrl + ^来切换VIM中的语言,并且相对满意.但似乎IdeaVim没有阅读或理解这个设置.用俄语停止键入某些内容(例如注释),切换到命令模式并卡住会非常烦人.有没有解决方法?
我使用Visual Studio 2005运行C++ Win32控制台应用程序时遇到了非常严重的错误.使用以下项目属性运行下面的代码(简化)时会出现问题:C++ | optimization | optimization |/O2(或/ O1,或/ Ox),C++ | optimization |整个程序优化|/GL,链接器|优化|/LTCG
#include "stdafx.h"
#include <iostream>
using namespace std;
const int MAXVAL=10;
class MyClass
{
private:
int p;
bool isGood;
public:
int SetUp(int val);
};
int MyClass::SetUp(int val)
{
isGood = true;
if (MAXVAL<val)
{
int wait;
cerr<<"ERROR, "<<MAXVAL<<"<"<<val<<endl;
cin>>wait;
//exit(1); //for x64 uncomment, for win32 leave commented
}
if (isGood) p=4;
return 1;
}
int _tmain(int argc, _TCHAR* argv[])
{
int wait=0, setupVal1=10, setupVal2=12;
MyClass classInstance1;
MyClass …Run Code Online (Sandbox Code Playgroud) 我的程序需要在文本文件中搜索单词,如果找到该单词,则打印/显示整行.例:
employee name date joined position project annual salary tom jones 1/13/2011 accountant pricing 55000 Susan lee 2/5/2007 Manager policy 70000
用户输入搜索词:
会计
程序搜索文本accountant.当它找到它时,它返回以下内容:
employee name date joined position project annual salary tom jones 1/13/2011 accountant pricing 55000
这是我提出的代码,但它不起作用.
void KeyWord(ifstream &FileSearch)
{
string letters;
int position =-1;
string line;
ifstream readSearch;
cout<<"enter search word ";
cin>>letters;
"\n";
FileSearch.open("employee");
if(FileSearch.is_open())
{
while(getline(FileSearch, line))
{
FileSearch>>line;
cout<<line<<endl;
position=line.find(letters,position+1);
if(position==string::npos);
if(FileSearch.eof())
break;
cout<<line<<endl;
}
}
cout<<"Cant find"<<letters<<endl;
}
Run Code Online (Sandbox Code Playgroud) c++ ×7
c ×2
assembly ×1
compiler-bug ×1
gcc ×1
gnu-make ×1
ideavim ×1
io ×1
perforce ×1
portability ×1
range-v3 ×1
refactoring ×1
vim ×1
visual-c++ ×1