17

This may be a minor question, but a solution would save me a lot of time and prevent mistakes.

I am working on a C++ project in Visual Studio. If I define a function in a class in a header file, say

void InitButton(int ButtonNum);

I usually copy and paste the signature to the cpp file. Then, I insert the class name, and replace the semi-colon with curly braces, like so:

void Button::InitButton() { 
}

However, I'll often forget the class name, or accidentally type it before the return type. This also happens for any static variables I need to define in code. This seems small, but piles up since I'm at the beginning phase of a project. Is there a quicker way to auto-generate these in Visual Studio C++? Or a best practice I'm missing out on?

EDIT: It appears this has been asked before: Auto-create implementation in Visual Studio C++ 2010

EDIT 2: The best solution for me appears here: http://www.radwin.org/michael/2011/05/10/stubgen/

Community
  • 1
  • 1
escapecharacter
  • 954
  • 2
  • 12
  • 29
  • Just, don't do that. You should only make this mistake a handful of times, then rarely (if ever) again. That said I'm sure there's some tool that will generate the source file out of the header declarations. – GManNickG Jul 31 '13 at 04:33
  • Visual Assist X will do this for you. `Right click -> Refactor (VA X) -> Create Implementation`. It's a visual studio addon, it is not free but well worth the price. It also offers the reverse, `Create Declaration` as well as many other features. – Borgleader Jul 31 '13 at 04:34
  • Your EDIT 2 should have been an answer, rather than editing it into the question (answering your own question is absolutely fine. We are trying to build a great Q&A site, and if it's a good answer, that will help. Feel free to add your answer to the question this is a duplicate of. – Martin Bonner supports Monica Mar 05 '17 at 12:32
  • 4
    Here's an effective solution: `alt+shift+f10` Details: https://stackoverflow.com/questions/1610011/creating-cpp-files-from-h-files-visual-studio/46909447#46909447 After years of being told that this was not possible without an external tool, I somehow stumbled on this and wrote up an answer. Please +1 if this happens to be useful - I've found this immensely refreshing. (I comment here since this thread seems to have most views of the mass of duplicate threads) – dk123 Oct 25 '17 at 02:11

2 Answers2

13

In Visual Studio 2015 there's the "Quick Actions" feature, a light bulb that shows up whenever you hover over a piece of code. In a header file one of the options is "Create definition of ... in ....cpp". This option generates the function definition in the corresponding header file.

mrbraitwistle
  • 414
  • 4
  • 5
  • 11
    in visual studio 2015 you can bind Edit.CreateDeclarationDefinition to a shortcut keystroke. ( Tools | Options -> Environment -> Keyboard) it does the same thing as the quick actions popup, but you can select multiple functions and press the shortcut keybinding to generate all the functions in one go. – jhbh Nov 27 '17 at 19:36
4

Visual Assist has a generator for this. I'm sure there are a lot of plugins that have the same functionality, but you might have to part with some cash for them. Visual Assist is well worth the money though, as it gives you a lot of functionality which I find invaluable. My killer features are the better function and method info, better auto complete and find file in projects (one key combination and you get a list of all files in all projects and can search for a particular file without knowing where in the hierarchy it lives).

Dominique McDonnell
  • 2,510
  • 16
  • 25
  • Alas, low on cash at the moment, but thanks for the suggestion. – escapecharacter Jul 31 '13 at 04:38
  • 37
    Isn't it absolute madness that we're here scouring the internet only to find out that an IDE that we paid hundreds of dollars for lacks even the most basic of convenience tools? –  Feb 19 '15 at 11:13
  • note: *find file in projects*-like functionality is actually builtin into VS and (imo) in a better way since it also looks for symbols which is handy for large codebases where you cannot exactly remember filename or typename. Command is `Edit.NavigateTo`. Bind this to a shortcut and just start typing. – stijn Apr 28 '15 at 08:24
  • @stijn yes, VS 2013 (released October 17, 2013) has this functionality, however I find that because it does both symbols and files it introduces a lot of noise and makes it harder to find what you are actually looking for, and it seems to do it on the fly, so is slow. I actually prefer Visual Assist's segmentation to Find File and Find Symbol which gives you the same functionality, but doesn't make you sift through both when you want one. And it seems 10x faster to me. – Dominique McDonnell Apr 28 '15 at 22:21
  • yes good points (VS2012 also has it btw, in a lesser form), it's definitely faster. It sort of depends what you are used to I guess, I don't mind the speed: usually by the time I've finished typing it has narrowed down enough and I find the live preview feature indispensable – stijn Apr 29 '15 at 09:27