There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
I get the following: error: invalid operands of types 'constant char*' and 'constant char*' to binary 'operator+'
After some googling i see that constant characters cannot be added together, but a constant character can be added to a string, but if, within my main(), i specify as follows:
I decided to challenge myself by using one function inside of another function. It took me a lot of debugging to get it straight, but it was worth it! My idea was to create a program which calculates kinetic energy from an object’s mass, displacement (that’s distance in a particular direction), and the time taken to make the movement. Here is the main.cpp file of my code:
I know this is an old comment, but I was scrolling through and trying to troubleshoot some projects as a learning exercise, and figured I’d post what I worked out.
I think the main problem is the arguments “A” and “B” aren’t passing as a string, they are passing as constant char*, meaning that they can’t be modified in any way. It’s something to do with the argument passing as a pointer, rather than an object. If you declare the string as an object outside of the statement, you can then pass the string as the argument to be concatenated.
// fns.hpp
template <typename T>
T add_together(T input1, T input2){
return input1 + input2;
}
I’d agree with your here. So far as I’m aware string literals always evaluate that way. So "A" is basically equivalent to const char name[] = {'A', '\0'}, a constant character array terminated by null (without the assigned name though, it’s just an lvalue). I think the literal would always be stored away as data at compile time anyway.
The major issue being that "A" is not a C++ string object at that point and when named inside the function it’s treated like an array name would be, a pointer to this first value in an array. So you cannot use + with another array pointer (it doesn’t even know how long the character array is at that point).
The literal evaluation is also why the following is perfectly valid-
where s1 is the first pointer in an array of values (like name is in the equivalent example). Like a pointer passed to a function though we don’t know the length from the pointer alone and rely on the null termination to prevent overflows (string literals are always null terminated so far as I’m aware).
Since the underlying object created by the string literal is immutable the following at least throws a warning and may fail to compile depending on current iso standard, compiler and flags-
char *s2 = "RED"; // <-- Bad practice even if the compiler allows it
Assuming it even complied, attempting to modify any values pointed to by s2 and the neighbouring const chars, e.g. s2[0] = 'B' would be either undefined or broken behaviour that would probably just lead to a crash.
Been stuck on this for a day now defiantly restarting the lesson to understand it better but what am i do doing wrong?
main.cpp:#include #include #include “fns.hpp”
int main() {
int length, width;
rectangle(length, width);
int area = rectangle(length, width);
double perimeter = rectangle(length, width);
std::cout << "The area of your rectangle is " << area<< "\n";
std::cout << "The perimeter of your rectangle is " << perimeter << "\n";
return 0;
}
fns.hpp:
//Delcarations
void rectangle(int& length, int& width);
int rectangle(int length, int width);
double rectangle(double length, double width);
fns.cpp:
//Definition
void rectangle(int& length, int& width){
std::cout << “Enter the length of your rectangle.\n”;
std::cin >> length;
std::cout << “Enter the width of your rectangle.\n”;
std::cin >> width;
}
int rectangle(int length, int width){
return length * width;
}