|
You have a name clash with std::swap . Try
#include <iostream>
using std::cout;
using std::endl;
template<class T>
void swap(T &x, T &y){
T temp = x;
x = y;
y = temp;
}
void fun(int m, int n, float a, float b){
cout << "m & n before swap: " << m << " " << n << endl;
swap(m, n);
cout << "m & n after swap: " << m << " " << n << endl;
cout << "i & j before swap: " << a << " " << b << endl;
swap(a, b);
cout << "i & j after swap: " << a << " " << b << endl;
}
int main()
{
fun(100, 200, 11.22, 33.44);
}
|
|
|
|
|
|
|
here in the below program i have tried to pass the values of 1d matrix to the object of class Vector by using constructor explicit call.
#include <iostream>
using namespace std;
const int size = 3;
class Vector {
int *v;
public:
Vector(){
v = new int[size];
for(int i=0; i<size; i++)
v[i] = 0;
}
Vector(int *a){
for(int i=0; i<size; i++){
v[i] = a[i] ;
}
}
int operator * (Vector &y){
int sum=0;
for(int i=0; i<size; i++)
sum += this->v[i] * y.v[i] ;
return sum;
}
void display(){
for(int i=0; i<size; i++)
cout << v[i] << " ";
cout << endl;
}
};
int main(){
int x[3] = {1, 2, 3};
int y[3] = {6, 3, 9};
Vector v1, v2;
v1 = y;
v2 = x;
cout << "v1 = ";
v1.display();
cout << "v2 = ";
v2.display();
cout << "v1 x v2 = " << v1 * v2 << endl ;
return 0;
}
Quote: cout << "v1 = ";
v1.display();
cout << "v2 = ";
v2.display();
the above portion of code doesn't works as expected it gives,
Output:
Quote:
6 3 9
6 3 9
v1 = 6 3 9
v2 = 6 3 9
v1 x v2 = 126
Expected:
Quote: Quote:
1 2 3
6 3 9
v1 = 1 2 3
v2 = 6 3 9
v1 x v2 = 39
Thank you
|
|
|
|
|
The program shouldn't run as is. In your second constructor, you aren't allocating the memory for the vector. (You also aren't deleting that memory in a destructor.)
Also note that you construct the v object in the default constructor. Since there is no assignment operator AND the second constructor is not marked explicit, it is creating a second instance of Vector and then member-wise copying that to the first, clobbering the allocation of v in the first instance. (Why your compiler is letting you get away with this is a mystery to me.)
So, add a destructor and then put "explicit" in front of the second constructor.
Then, remove the assignments and replace the declaration line with: v1(x), v2(y).
(It's still messy code, but in a debugger, you can now see how you are explicitly calling the second constructor and never the first.)
|
|
|
|
|
|
Since you know at compile time the array size, why don't you allocate it on the stack?
#include <iostream>
#include <array>
using namespace std;
constexpr size_t SIZE = 3;
class Vector
{
array<int,SIZE> x{};
public:
Vector (const array<int,SIZE> & a)
{
for (size_t n=0; n<SIZE; ++n)
x[n] = a[n];
}
int operator * (const Vector & v) const
{
int result = 0;
for (size_t n=0; n<SIZE; ++n)
result += x[n] * v.x[n];
return result;
}
friend ostream & operator << ( ostream & os, const Vector & v);
};
ostream & operator << ( ostream & os, const Vector & v)
{
for (const auto & i : v.x)
os << i << " ";
return os;
}
int main()
{
array<int, SIZE> x{1,2,3};
array<int, SIZE> y{6,3,9};
Vector v1{x};
Vector v2{y};
cout << "v1 " << v1 << endl;
cout << "v2 " << v2 << endl;
cout << "v1*v2 = " << (v1*v2) << endl;
}
|
|
|
|
|
I'm converting a project from VS2005 to VS2017 and have many errors:
I'm getting many errors for items not being members of System
Windows
Forms
Drawing
Panel
Button
etc....
Is there something missing from my old code or is this not available in VS2017 or is there something that wasn't included?
I think the app creates a form....
Any help to debug would be much appreciated.
Jim
|
|
|
|
|
Is it a managed C++ project type?
|
|
|
|
|
|
Then it would be better to post in the Managed C++/CLI Forum.
|
|
|
|
|
This is silly , but most I/O devices spec sheets data are written as "hex".
If I want to pass this
char *TXBuffer = (char*) 0x04;
to a function it has to be type casted.
Like so
<pre lang="c++">
char *TXBuffer = (char*) 0x04;
int *TEST = (int*)0x04;</pre>
If not I'll get "invalid conversion".
Cheers
Vaclav
|
|
|
|
|
Vaclav_ wrote: char *TXBuffer = (char*) 0x04; No you don't need to do that, and you shouldn't because it is wrong. You are trying to pass the value 0x04 as the buffer address, which will cause an access violation.
If you need a buffer containing the hex value, and you want to send that buffer's address to a function then you need to do one or other of the following:
unsigned char TXBuffer = 0x04; function(&TXBuffer);
unsigned char TXBuffer[] = { 0x04, 0 }; function(TXBuffer);
xxx function(unsigned char* buffer)
{
unsigned char value = *buffer; buffer++; value = *buffer; }
|
|
|
|
|
Thanks Richard, you are a pal.
Appreciate your comments.
|
|
|
|
|
Hi, I wrote a small c program on a microcontroller, 32bit.
I met a weird problem.
I wrote code like:
const char strtest[] = "Test123\r\n"; UART_print(strtest);
the output is fine, I can see the output on Teraterm(RS232 interface tool), like:
Test123
Test123
..
But when I try code:
char str123[] = "Test123\r\n";
UART_print(strtest);
The output is only the first element:
TTTTTTTT
Any hint is appreciated.
|
|
|
|
|
focusdoit wrote: char str123[] = "Test123\r\n";
UART_print(strtest); Where is str123 used?
What is the signature of UART_print() ?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Please show the code for the UART_print function.
|
|
|
|
|
focusdoit wrote: But when I try code:
char str123[] = "Test123\r\n";
UART_print(strtest);
You defined the variable str123 but you print the strtest. Where and how is this strtest defined?
|
|
|
|
|
It's memory allocation problem. the MCU has 2 cpu inside, and one set of peripheral units, compiler put str123 in DMA Access prohibited area.
Embedded programming, too many tricks.
|
|
|
|
|
I like to rebuild my Raspberry Pi code to have "top down" structure.
On top would be the device "primitives" class - for example LCD draw circle
In middle I like to have hardware specifics, maybe even protocol (SPI,I2C) specifics - BCMx processor
And finally the access to I/O "pins"
I was thinking of "cascaded C++ " hierarchy but it would not be C++ hierarchy for real.
Or just simple top class having supporting class as variable. Not sure how flexible that would be.
The idea is to be able to add another "top" device and reuse the supporting classes.
I hope this makes sense and if I have asked this before - just ignore OF.
Thanks
Cheers Vaclav
|
|
|
|
|
It actually more complex than that because on a Pi2,Pi3 you have 4 cores so you will have spinlocks/semaphores on all the devices.
What are you doing with cores 1,2,3 at the moment are they still parked or in use?
I assume your code isn't under linux given you have dragged the devices out to objects
Assuming baremetal I would strongly suggest you look at circle
GitHub - rsta2/circle: A C++ bare metal environment for Raspberry Pi with USB[^]
I am not sure you can develop much of a hierarchy it all needs to be fairly flat if you want to use the multicores, welcome to multicore programming
In vino veritas
modified 9-May-18 11:28am.
|
|
|
|
|
I have been taking the free CS50x course from Harvard and just completed the Mario more comfortable problem. This is my code and I wanted to know how the right side of hashes are left aligned even though the code is the same for both left and right hashes.? To be honest I’m new to this and thought it would simply print out two right aligned pyramids with a gap in between.
// Prints bricks, sized as specified by user
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Prompt user for a positive number to use as height of pyramid
int h;
do
{
h = get_int("Height: ");
}
while (h < 0 || h > 23);
// Print out this many rows
for (int i = 0; i < h; i++)
{
// Print out this many spaces
for (int j = 0; j < h - i; j++)
{
printf(" ");
}
// Print left hashes
for (int j = 0; j < i + 2; j++)
{
printf("#");
}
// Print gap
printf(" ");
// Print right hashes
for (int j = 0; j < i + 2; j++)
{
printf("#");
}
printf("\n");
}
}
|
|
|
|
|
Faith Burnett wrote: To be honest I’m new to this and thought it would simply print out two right aligned pyramids with a gap in between. Those two for() loops are printing the same NUMBER of hashes from where the stdout stream happens to be at the point the for() loop starts printing. Since the stdout stream is at "column" 0 before the first for() loop, the first set of hashes is right justified (preceded with spaces), and since the stdout stream is at "column" H+2 after the first two inner for() loops run, the second set of hashes always starts in the same "column."
You might want to consider changing your for() loop boundaries so that the pyramid has a "pointed" top at level 1, and no leading/trailing spaces at level h :
for (int i = 0; i < h; i++)
{
...
for (int j = 0; j < h - i - 1; j++)
...
for (int j = 0; j < i + 1; j++)
...
for (int j = 0; j < i + 1; j++)
...
}
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
modified 6-May-18 0:28am.
|
|
|
|
|
Thanks for the help David!
|
|
|
|
|
I like to verify actuall value passed from char array to struct pointer
buf array declaration
unsigned char buf[32]; // = { 0x00, 0x04 };
value passed unsigned char 0x04;
buf[0] = command; // passed 0x04 software reset
transmit buffer assignment
xfer[0].tx_buf = (unsigned long) buf;
Now I like to use cout to print the actual value in tx_buf.
From struct declaration I know "tx_buf" is a unsigned long pointer.
Not sure how to access the required value
this gives me the pointer as expected:
<pre lang="c++">cout << "xfer[0].tx_buf " << xfer[0].tx_buf<< endl;
Once I know how to get first value I can probably figure out how to get the whole buf[32] array values.
Thanks for help
Cheers Vaclav
|
|
|
|