I am using Visual Studio 2012 and have Smart indentation turned on1 for C++ files.2 I would like to customize Smart indentation's behavior so that it formats the code I enter so that it complies with my company's coding style.
How can I customize all the minute aspects of how Smart indentation behaves?
For example, when I enter this code, Smart indentation formats it exactly like this:
#include <cstdlib>
#include <string>
using namespace std;
struct Foo
{
const string mA;
const int mB;
const string mC;
Foo(const string& a,
const int b,
const string& c)
:
mA(a),
mB(b),
mC(c)
{
}
};
int main()
{
}
Most of this is what I want, except for the colon introducing the initializer list, the first item in the initializer list, and the indentation level of the constructor's body. I want these formatted like this, and I want Visual Studio to do it for me automatically:
Foo(const string& a,
const int b,
const string& c)
:
mA(a),
mB(b),
mC(c)
{
}
How can I customize Smart indentation's behavior? I'd prefer to not use any external tools like Visual Assist X.
1: Via Tools > Options > Text Editor > C/C++ > Tabs > Indenting
2: I also have tabstops set to 4, with spaces inserted.
Look into the MS Visual Studio SDK, found here:
http://msdn.microsoft.com/en-us/library/bb139565.aspx
In particular you want to override HandleSmartIndent in the VewFilter class:
http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.package.viewfilter.handlesmartindent.aspx
This gets called whenever you press the Enter key in the editor. Unfortunately, it's not as easy as just changing some rules in a config dialog.
An ugly solution is this:
Foo(const string& a,
const int b,
const string& c)
: mA(a)
, mB(b)
, mC(c)
{
}
Which, for some abominable reason, is the only way I've ever seen to get VS to indent that mess properly.
Related
I noticed that the auto-complete options like [Cltr + space] in Visual Studio IDE can only paste function names.
Here is an c++ example code:
class TestClass
{
public:
explicit TestClass();
virtual ~TestClass();
void callThisMethod(int a, char ch, string s);
}
// TestClass.cpp file
void TestClass:callThisMethod(int a, char ch, string s){.....}
// main.cpp file:
TestClass tc;
tc.callThisMethod(int a, char ch, string s); // here
So is there any way to bring function calls with all necessary parameters.
The Visual Studio correctly designed for what it does:
The intellisense will make you on the right track by keeping showing the function signature, so that you would know which parameters are actually required. For an instance:
From the example, it can be clearly seen here that as longer as you type and provide the arguments, the popup will help you to go through the function signature.
Thus, it doesn't seems the feature you're asking is currently available, neither it's required due to the intellisense which actually helps you a lot in this situation.
A program im writing successfully runs on the IDE visual studio code. However, when attempting to use the Bitvise SSH client to run my program I get a list of errors that I myself cannot understand the problem for. Bitvise it another way to access the CMD client from a remote server, for all intenstive purposes it acts the same as windows CMD. I will provide a screen cap of the errors and a full run down of the parts of my program that I believe are causing the errors. If any further code is required please feel free to ask.
errors screen cap
This error report shows a common error, with something being a placeholder for all instances.
multiple definition of `something' /tmp/ccBhjFYn.o:(.bss+0x0): first defined here
This error DOES NOT happen in visual studio code
From this error report it can be seen the issue is found within driver.cpp and my header.h file. For this reason i wont provide a minimal code for these files, but they are small enough to not require one.
MAIN
int main()
{
Customer c;
Part p;
Builder b;
const string fileName = "Parts.txt";
auto partsVec = readpartFile();
auto customerVec = readcustomerFile();
auto builderVec = readbuilderFile();
fexists(fileName);
complexity(c, partsVec);
robotComplexity(partsVec,customerVec);
writeFile(buildAttempt(b, complexity(c, partsVec), variability(customerVec, builderVec)));
return 0;
}
HEADER FILE
#include <vector>
#include <string>
struct Customer {
std::string customerName;
std::string projectName;
std::string listofParts;
} myCustomer;
struct Part {
char partCode;
std::string partName;
int maximum;
int minimum;
int complexity;
} myPart;
struct Builder {
std::string builderName;
int ability;
int variability;
} myBuilder;
bool fexists(const std::string filename);
std::vector<Part> readpartFile();
std::vector<Customer> readcustomerFile();
std::vector<Builder> readbuilderFile();
float complexity(const Customer& c, const std::vector<Part>& parts);
void robotComplexity(const std::vector<Part>& vecB, const std::vector<Customer>& vecC);
double variability(const std::vector<Customer>& customerList, const std::vector<Builder>& builderList);
std::vector<double> buildAttempt(Builder b, double variaiblity, double complexityRobot);
void writeFile(std::vector<double> build);
Thankyou for any help. This question may be hard to understand and follow but i did try my best. Any sugguestions to help improve this question are welcome but please be friendly :)
This in header.h
struct Customer {
std::string customerName;
std::string projectName;
std::string listofParts;
} myCustomer;
Is a definiton of a global variable myCustomer. As such it does not belong in a header file.
Change the header file to this
struct Customer {
std::string customerName;
std::string projectName;
std::string listofParts;
};
extern Customer myCustomer; // global variable declaration
Then to one of your cpp files (I suggest implementation.cpp) add this
Customer myCustomer; // global variable definition
Or you could just do away with global variables completely (the best solution).
NOTE in some of my comments above, I said you have global variable declarations in your header file. What I meant was you have global variable definitions in your header file. The difference between a definition and a declaration is what is crucial here. It's fine to put declarations in a header file, it's wrong to put a definition. Sorry for any confusion.
I am trying to compile a project that uses log4cxx, a c++ logging framework. Once I downloaded the framework and added it as a dependency, however, I get this error:
'Locale' : looks like a function definition, but there is no parameter list; skipping apparent body
This happens in locale.h which looks like this:
#ifndef _LOG4CXX_HELPERS_LOCALE_H
#define _LOG4CXX_HELPERS_LOCALE_H
#include <log4cxx/logstring.h>
namespace log4cxx
{
namespace helpers
{
class LOG4CXX_EXPORT Locale
{
public:
Locale(const LogString& language);
Locale(const LogString& language, const LogString& country);
Locale(const LogString& language, const LogString& country,
const LogString& variant);
const LogString& getLanguage() const;
const LogString& getCountry() const;
const LogString& getVariant() const;
protected:
Locale(const Locale&);
Locale& operator=(const Locale&);
const LogString language;
const LogString country;
const LogString variant;
}; // class Locale
} // namespace helpers
} // namespace log4cxx
#endif // _LOG4CXX_HELPERS_LOCALE_H
Is there anything wrong with this syntax? I am not knowledgeable in C++ at all but I do not see what would be wrong with it.
The one single thing on the internet I could find was this: https://issues.apache.org/jira/browse/LOGCXX-147 which makes perfect sense because I am targetting a windows ce device (although, using Visual Studio 2008 instead of 2005). The suggestion there is to include log4cxx/logstring.h inside "the cpp file" which I assume they mean locale.cpp although if I am clearly misinterpreting this please let me know. It said that would get rid of the compilation error but unfortunately I did that and it did not do anything to help :(.
I know it may be a bit of a longshot but does anyone know how one might fix this?
I want to create a MFC project in Visual Studio where, on button action to go through various functions(like 'prime'). I don't know where to write these function and also the global variables i need for them(p and flag). I have searched on the internet and tried more methods(like declaring them in .h files and so on, but didn't worked). Take in my mind that i very new to this. Here is my code:
long int p,flag;
void CMFCApplication1Dlg::OnBnClickedClickMe()
{
flag = prime(p);
//more code
}
int prime(long int pr)
{
//code
}
So this is possibly the strangest thing I've seen recently and was curious how this could happen. The compiler gave me an error saying that std::string is undefined when used as a return type but not when used as a parameter in methods of a class!
#pragma once
#include <string>
#include <vector>
// forward declarations
class CLocalReference;
class CResultSetHandle;
class MyClass
{
public:
MyClass() {}
~MyClass {}
void Retrieve(const CLocalReference& id, CResultSetHandle& rsh, std::string& item); // this is fine
const std::string Retrieve(const CLocalReference& id, CResultSetHandle& rsh); // this fails with std::string is undefined?!?!
};
Doing a Rebuild All it still happened I had to choose clean solution and then Rebuild All again after for the universe to realign. While it's resolved for the moment I'd still like to know what could have caused this because I'm at a loss as to why when there should be no conflicts especially when I always use fully qualified names for STL.
This is probably a compiler bug. I have seen several others in VS2010.